Hone logo
Hone
Problems

Jest Path Mapping Challenge: Resolving Module Imports

Jest's path mapping feature allows you to configure how Jest resolves module imports, enabling you to work with projects that have unconventional directory structures or use aliases for modules. This challenge focuses on implementing a function that generates the Jest configuration object for path mappings based on a provided mapping definition. This is crucial for maintaining clean and maintainable codebases, especially in larger projects.

Problem Description

You are tasked with creating a TypeScript function generateJestPathMappingConfig that takes a mapping definition as input and returns a Jest configuration object containing the moduleNameMapper property. The mapping definition is an object where keys are module aliases (strings) and values are regular expressions representing the corresponding file paths. The function should construct a config object with the moduleNameMapper property set to an object representing the mapping.

Key Requirements:

  • The function must accept a single argument: mappingDefinition (an object).
  • The mappingDefinition object will have string keys (aliases) and regular expression values (paths).
  • The function must return a Jest configuration object with a moduleNameMapper property.
  • The moduleNameMapper property should be an object where keys are the aliases from the mappingDefinition and values are the corresponding regular expressions.
  • The returned object should only contain the moduleNameMapper property.

Expected Behavior:

The function should correctly transform the input mapping definition into the required Jest configuration format. It should handle empty mapping definitions gracefully.

Edge Cases to Consider:

  • Empty mappingDefinition object.
  • Regular expressions in the mappingDefinition are valid. No validation of the regex is required.
  • Aliases are strings.
  • The function should not modify the original mappingDefinition object.

Examples

Example 1:

Input: {
  '^@components': '<rootDir>/src/components',
  '^@utils': '<rootDir>/src/utils'
}
Output: {
  "moduleNameMapper": {
    "^@components": "<rootDir>/src/components",
    "^@utils": "<rootDir>/src/utils"
  }
}
Explanation: The input mapping is directly translated into the Jest moduleNameMapper format.

Example 2:

Input: {}
Output: {
  "moduleNameMapper": {}
}
Explanation: An empty mapping definition results in an empty moduleNameMapper.

Example 3:

Input: {
  '^@services/(.*)': '<rootDir>/src/services/$1'
}
Output: {
  "moduleNameMapper": {
    "^@services/(.*)": "<rootDir>/src/services/$1"
  }
}
Explanation: Handles regular expressions with capturing groups.

Constraints

  • The mappingDefinition object will only contain string keys and regular expression values.
  • The regular expressions are assumed to be valid.
  • The function must return an object with only the moduleNameMapper property.
  • The function must be written in TypeScript.
  • Performance is not a primary concern for this challenge; readability and correctness are prioritized.

Notes

  • Think about how to efficiently create the moduleNameMapper object from the input mappingDefinition.
  • The config object should be a plain JavaScript object, not a Jest configuration object with other properties. Only the moduleNameMapper is required.
  • Consider using a simple loop to iterate through the mappingDefinition and construct the moduleNameMapper object.
Loading editor...
typescript