Hone logo
Hone
Problems

Jest Coverage Collection from Specific Files

This challenge focuses on implementing a function collectCoverageFrom that accurately gathers coverage data from a specified set of files. Accurate coverage reporting is crucial for identifying gaps in test coverage and ensuring code quality, and this function is a core component of a Jest configuration. You'll need to parse a configuration object and extract the relevant file paths to use for coverage collection.

Problem Description

You are tasked with implementing the collectCoverageFrom function. This function takes a Jest configuration object as input and extracts the collectCoverageFrom array. The function should return a new array containing only the file paths specified in the collectCoverageFrom array of the configuration. If the collectCoverageFrom property is missing or empty in the configuration, the function should return an empty array.

Key Requirements:

  • The function must handle cases where the collectCoverageFrom property is not present in the configuration.
  • The function must handle cases where the collectCoverageFrom property is an empty array.
  • The function should return a new array, not modify the original configuration object.
  • The function should correctly extract the file paths from the collectCoverageFrom array.

Expected Behavior:

Given a Jest configuration object, the function should return an array of strings, where each string represents a file path to be included in coverage collection.

Edge Cases to Consider:

  • Configuration object is null or undefined. (Treat as an empty object)
  • collectCoverageFrom is null or undefined. (Treat as an empty array)
  • collectCoverageFrom contains non-string values. (Ignore these values - only include strings)
  • Empty strings in collectCoverageFrom. (Include them as empty strings in the output)

Examples

Example 1:

Input: { collectCoverageFrom: ['src/**/*.ts', 'src/**/*.tsx'] }
Output: ['src/**/*.ts', 'src/**/*.tsx']
Explanation: The configuration contains a non-empty `collectCoverageFrom` array with valid file paths. The function should return this array.

Example 2:

Input: { transform: { compiler: 'typescript' } }
Output: []
Explanation: The configuration does not contain a `collectCoverageFrom` property. The function should return an empty array.

Example 3:

Input: { collectCoverageFrom: [] }
Output: []
Explanation: The configuration contains an empty `collectCoverageFrom` array. The function should return an empty array.

Example 4:

Input: { collectCoverageFrom: ['src/**/*.ts', 123, 'src/**/*.tsx', null, ''] }
Output: ['src/**/*.ts', 'src/**/*.tsx', '']
Explanation: The configuration contains a mix of valid and invalid values in the `collectCoverageFrom` array. The function should only include the string values.

Constraints

  • The input configuration object will be a JavaScript object.
  • The collectCoverageFrom property, if present, will be an array.
  • The file paths within the collectCoverageFrom array can be strings or other data types.
  • The function must be performant enough to handle reasonably sized configuration objects (up to a few hundred properties).
  • The function should not throw errors.

Notes

  • Consider using optional chaining (?.) to safely access the collectCoverageFrom property.
  • The function should be robust and handle various input scenarios gracefully.
  • Focus on clarity and readability in your code.
  • The goal is to extract the file paths, not to validate them. Assume the provided paths are valid for Jest's coverage collection.
  • You are not required to write Jest tests for this function, but you should mentally consider how you would test it.
Loading editor...
typescript