@bestcodetools/jest-tsconfig-paths-mapper
Version:
A utility library for generating Jest's moduleNameMapper based on TypeScript's tsconfig.json path mappings.
60 lines (35 loc) • 2 kB
Markdown
# @bestcodetools/jest-tsconfig-paths-mapper
A utility library for generating Jest's `moduleNameMapper` based on TypeScript's `tsconfig.json` path mappings.
## Installation
Install the package using npm:
```shell
npm install @bestcodetools/jest-tsconfig-paths-mapper
```
## Usage
1. Create a `jest.config.js` file in the root of your project if you don't have one already.
2. Import the `generateModuleNameMapper` function from the `@bestcodetools/jest-tsconfig-paths-mapper` package at the top of your `jest.config.js` file:
```javascript
const { generateModuleNameMapper } = require('@bestcodetools/jest-tsconfig-paths-mapper');
```
3. Define the `moduleNameMapper` property in the `module.exports` object of your `jest.config.js` file:
```javascript
module.exports = {
// Other Jest configuration options...
moduleNameMapper: generateModuleNameMapper(),
};
```
4. Save the changes to the `jest.config.js` file.
The `generateModuleNameMapper` function automatically reads the `tsconfig.json` file located in the root of your project and generates the `moduleNameMapper` object based on the path mappings defined in the `compilerOptions` section.
Now, when you run your Jest tests, the module name mappings will be resolved correctly based on the `tsconfig.json` paths.
## Custom tsconfig.json Path
If your `tsconfig.json` file is located in a different directory or has a different name, you can provide the path as an argument to the `generateModuleNameMapper` function:
```javascript
module.exports = {
// Other Jest configuration options...
moduleNameMapper: generateModuleNameMapper('/path/to/custom/tsconfig.json'),
};
```
Make sure to replace `/path/to/custom/tsconfig.json` with the actual path to your custom `tsconfig.json` file.
## License
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to customize the README.md file further to include additional sections or provide more detailed instructions as needed.