Note
- I am able to use aliases in my TypeScript file.
- Unfortunately, I cannot use aliases in my test files (*.test.ts).
My Configuration
1. Jest.config.ts
import type { Config } from '@jest/types';
const config: Config.InitialOptions = {
verbose: true,
preset: 'ts-jest',
clearMocks: true,
collectCoverage: true,
coverageDirectory: 'tests/coverage',
collectCoverageFrom: ['<rootDir>/src/**/*.ts?'],
coverageProvider: 'v8',
moduleFileExtensions: [
'js',
'mjs',
'cjs',
'jsx',
'ts',
'tsx',
'json',
'node',
],
rootDir: '.',
moduleNameMapper: {
'/^@(.*)$': '<rootDir>/src/$1',
},
modulePathIgnorePatterns: ['tests/coverage'],
testMatch: ['**/tests/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
testEnvironment: 'node',
};
export default config;
2. tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["ESNext", "DOM", "es5", "es2015.promise"],
"baseUrl": "./",
"module": "esnext",
"sourceMap": true,
"moduleResolution": "node",
"noImplicitAny": true,
"allowSyntheticDefaultImports": true,
"typeRoots": ["./node_modules/@types/"],
"types": ["jest", "node"],
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
When I execute this command in the terminal:
jest --config ./jest.config.ts --coverage
My Test Failed:
https://i.stack.imgur.com/fJOyv.png
What happened? I have configured it in jest.config.ts.
moduleNameMapper: {
'/^@(.*)$': '<rootDir>/src/$1',
},
- The first time out of my TypeScript type check output.
https://i.stack.imgur.com/IzHiu.png
I can't believe it. There was a statement recently - why is it happening now? Please help me.