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
mappingDefinitionobject will have string keys (aliases) and regular expression values (paths). - The function must return a Jest configuration object with a
moduleNameMapperproperty. - The
moduleNameMapperproperty should be an object where keys are the aliases from themappingDefinitionand values are the corresponding regular expressions. - The returned object should only contain the
moduleNameMapperproperty.
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
mappingDefinitionobject. - Regular expressions in the
mappingDefinitionare valid. No validation of the regex is required. - Aliases are strings.
- The function should not modify the original
mappingDefinitionobject.
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
mappingDefinitionobject 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
moduleNameMapperproperty. - 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
moduleNameMapperobject from the inputmappingDefinition. - The
configobject should be a plain JavaScript object, not a Jest configuration object with other properties. Only themoduleNameMapperis required. - Consider using a simple loop to iterate through the
mappingDefinitionand construct themoduleNameMapperobject.