`moduleNameMapper` settings in jest.config.js doesn't work on CircleCI
tsconfig-paths-jest
is not usable in Jest >23. For current Jest 26 I got it working via: https://kulshekhar.github.io/ts-jest/user/config/#paths-mapping
jest.config.js
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' } )
};
tsconfig.json
"compilerOptions"
"baseUrl": "./src",
"paths": {
"@models/*": [ "./models/*" ],
"@inputs/*": [ "./inputs/*" ],
"@tests/*": [ "./__tests__/*" ],
"@resolvers/*": [ "./resolvers/*" ],
"@seeds/*": [ "./seeds/*" ],
"@index": [ "./index.ts" ],
"@ormconfig":[ "../ormconfig.ts" ]
},
I've finally found out its solution.
According to this issue, I use tsconfig-paths
and tsconfig-paths-jest
.
So my setting-files have changed like below.
tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "dist/",
"allowJs": true,
"checkJs": true,
"moduleResolution": "node",
"sourceMap": true,
"noImplicitAny": true,
"target": "es6",
"module": "commonjs",
"lib": ["es5", "es6", "dom"],
"jsx": "react",
"strict": false,
"removeComments": true,
"types": ["node", "jest"],
"paths": {
"Components/*": ["src/components/*"]
}
},
"typeRoots": ["node_modules/@types"],
"include": ["src/**/*"],
"exclude": ["node_modules", "__tests__"]
}
jest.config.js
/* eslint-disable import/order */
/* eslint-disable @typescript-eslint/no-var-requires */
const tsconfig = require("./tsconfig.json");
const moduleNameMapper = require("tsconfig-paths-jest")(tsconfig);
module.exports = {
globals: {
"ts-jest": {
tsConfig: "tsconfig.json",
diagnostics: true
},
NODE_ENV: "test"
},
setupFilesAfterEnv: [`${__dirname}/src/setupTests.ts`],
moduleDirectories: ["node_modules", 'src'],
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
transform: {
"^.+\\.tsx?$": "ts-jest"
},
verbose: true,
moduleNameMapper
};
And my tests worked well in CircleCI, but I still have no idea why these tests had worked in my local before this solution.