Hone logo
Hone
Problems

Testing with Fixture Data in Jest (TypeScript)

Testing with fixture data is a crucial practice for writing robust and reliable unit tests. This challenge focuses on effectively utilizing fixture data within your Jest tests written in TypeScript to isolate units of code and ensure predictable behavior. You'll be creating and using fixture data to test a function that processes user data.

Problem Description

You are tasked with writing Jest tests for a TypeScript function called processUserData. This function takes an array of user objects as input and returns a new array where each user's age is incremented by 1. To ensure your tests are isolated and repeatable, you need to create and utilize fixture data – predefined, static data sets – within your tests. Your tests should cover both successful processing and a scenario where the input array is empty.

What needs to be achieved:

  1. Create a TypeScript function processUserData that takes an array of User objects and returns a new array with each user's age incremented by 1.
  2. Define a User type to represent the structure of user data.
  3. Create fixture data (arrays of User objects) to use in your Jest tests.
  4. Write at least two Jest tests: one to verify the successful processing of user data and another to handle the edge case of an empty input array.

Key Requirements:

  • The processUserData function must be implemented correctly.
  • Fixture data must be clearly defined and used within the tests.
  • Tests must be readable and well-structured.
  • Tests must accurately verify the expected behavior of processUserData.

Expected Behavior:

  • When given a valid array of User objects, processUserData should return a new array with each user's age incremented by 1.
  • When given an empty array, processUserData should return an empty array.

Edge Cases to Consider:

  • Empty input array.
  • Ensure the original array is not mutated. processUserData should return a new array.

Examples

Example 1:

Input:
const users = [
  { id: 1, name: "Alice", age: 30 },
  { id: 2, name: "Bob", age: 25 }
];

Output:
[
  { id: 1, name: "Alice", age: 31 },
  { id: 2, name: "Bob", age: 26 }
]

Explanation: Each user's age has been incremented by 1.

Example 2:

Input: []

Output: []

Explanation: An empty array is returned when the input is an empty array.

Constraints

  • The processUserData function must be pure (no side effects).
  • The User type must have id (number), name (string), and age (number) properties.
  • Tests should be written using Jest and TypeScript.
  • The solution should be well-formatted and easy to understand.

Notes

Consider using toEqual in your Jest assertions to compare arrays of objects. Remember that fixture data is intended to be static and predictable, allowing you to isolate the unit under test. Think about how to create reusable fixture data to avoid repetition in your tests. The function should not modify the original array; it should return a new one.

Loading editor...
typescript