Hone logo
Hone
Problems

Generate a Test Matrix for Unit Tests

This challenge focuses on creating a flexible and reusable mechanism to generate various test scenarios for your unit tests using Jest. This is particularly useful when testing functions that accept multiple parameters with different possible values, ensuring comprehensive coverage without repetitive test code.

Problem Description

Your task is to implement a function that takes a configuration object and generates an array of test objects. Each test object should represent a unique combination of input parameters based on the provided configuration. This generated array will then be used with Jest's .each() method to run the same test logic with different inputs.

What needs to be achieved:

Implement a TypeScript function generateTestMatrix that produces an array of objects. Each object in the array should represent a single test case, with properties corresponding to the keys defined in the input configuration.

Key requirements:

  • The function should accept a configuration object where keys represent parameter names and values are arrays of possible values for those parameters.
  • The function should return an array of objects, where each object is a complete set of parameters for a single test case.
  • All possible combinations of parameter values must be generated.
  • The output should be suitable for direct use with jest.each().

Expected behavior:

Given a configuration like { a: [1, 2], b: ['x', 'y'] }, the function should return [{ a: 1, b: 'x' }, { a: 1, b: 'y' }, { a: 2, b: 'x' }, { a: 2, b: 'y' }].

Edge cases to consider:

  • A parameter with an empty array of values.
  • An empty configuration object.
  • A configuration object with only one parameter.

Examples

Example 1:

Input: {
  userId: [101, 102],
  status: ['active', 'inactive']
}
Output: [
  { userId: 101, status: 'active' },
  { userId: 101, status: 'inactive' },
  { userId: 102, status: 'active' },
  { userId: 102, status: 'inactive' }
]
Explanation: This generates all four possible combinations of userId and status.

Example 2:

Input: {
  role: ['admin', 'user', 'guest']
}
Output: [
  { role: 'admin' },
  { role: 'user' },
  { role: 'guest' }
]
Explanation: When there's only one parameter, each value forms a separate test case.

Example 3:

Input: {
  type: ['A', 'B'],
  value: [10, 20],
  enabled: [true]
}
Output: [
  { type: 'A', value: 10, enabled: true },
  { type: 'A', value: 20, enabled: true },
  { type: 'B', value: 10, enabled: true },
  { type: 'B', value: 20, enabled: true }
]
Explanation: Parameters with a single value are still included in combinations.

Example 4: (Edge Case)

Input: {}
Output: []
Explanation: An empty configuration results in an empty test matrix.

Example 5: (Edge Case)

Input: {
  level: ['debug', 'info'],
  message: []
}
Output: []
Explanation: If any parameter has an empty array of values, no combinations can be formed, resulting in an empty matrix.

Constraints

  • The input configuration object will have string keys.
  • The values associated with the keys will be arrays of any primitive type (string, number, boolean) or null/undefined.
  • The total number of generated test cases should not exceed a reasonable limit for unit testing (e.g., avoid generating millions of tests, though explicit numerical limits are not strictly enforced here).
  • The function should be implemented in TypeScript.

Notes

  • Consider how to handle parameters with an empty array of values.
  • Think about the base case for your generation logic.
  • This is a great opportunity to practice recursion or iterative approaches for combination generation.
  • The output format should be an array of objects, suitable for jest.each(['%j'], data) or jest.each(data). For simplicity in this challenge, assume keys are sufficient for jest.each(data).
Loading editor...
typescript