Jest Import Interception: Mocking Module Resolutions
Import interception in Jest allows you to control which modules are loaded when your code imports them. This is incredibly useful for mocking dependencies, isolating units of code for testing, and simulating different module behaviors without modifying the original source code. This challenge will guide you in creating a Jest setup that intercepts module imports and replaces them with your mock implementations.
Problem Description
You need to create a Jest setup file (e.g., setup.ts or jest.setup.ts) that intercepts module import calls and replaces them with mock modules. The interception should work based on the module's path. Specifically, you'll be provided with a base path and a module name. The setup should mock the module located at basePath + '/' + moduleName. The mock should simply export an empty object {}.
Key Requirements:
- Dynamic Mocking: The setup should dynamically mock modules based on the
basePathandmoduleNameprovided. - Empty Object Mock: The mocked module should export an empty object (
{}). This provides a safe default behavior when a module is mocked. - Jest Compatibility: The setup file must be compatible with Jest and correctly configure the mocking behavior.
- No External Dependencies: The solution should only use Jest's built-in mocking capabilities.
Expected Behavior:
When a module is imported that matches the intercepted path, Jest should resolve the import to the mock module (an empty object). If the module is not intercepted, the original module should be loaded as usual.
Edge Cases to Consider:
- What happens if the
basePathis empty or contains invalid characters? (Assume it will always be a valid path string for this challenge). - What happens if the
moduleNameis empty or contains invalid characters? (Assume it will always be a valid module name string for this challenge). - How to ensure the mock is applied correctly during test execution?
Examples
Example 1:
Input: basePath = 'src/components', moduleName = 'Button.ts'
Output: When `import Button from './Button.ts';` is encountered, Jest should resolve to `{}`.
Explanation: The setup intercepts the import of `src/components/Button.ts` and replaces it with an empty object.
Example 2:
Input: basePath = 'utils', moduleName = 'api.js'
Output: When `import api from '../utils/api.js';` is encountered, Jest should resolve to `{}`.
Explanation: The setup intercepts the import of `utils/api.js` and replaces it with an empty object.
Example 3:
Input: basePath = '', moduleName = 'config.json'
Output: When `import config from './config.json';` is encountered, Jest should resolve to `{}`.
Explanation: The setup intercepts the import of `config.json` and replaces it with an empty object.
Constraints
- The solution must be written in TypeScript.
- The solution must be a valid Jest setup file (e.g.,
setup.tsorjest.setup.ts). - The solution should not modify the original source code of the project being tested.
- The solution should only use Jest's built-in mocking capabilities.
- The mocked module should always export an empty object
{}.
Notes
Consider using jest.mock with a dynamic mock factory. The mock factory function will receive the module name as an argument and should return the mock module (an empty object in this case). Think about how to construct the full module path dynamically based on the basePath and moduleName. Remember that Jest's mocking system can be a bit tricky, so pay close attention to how module resolution works.