Here’s my jest
config inside of package.json
:
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"moduleDirectories":["node_modules", "src"], // tried to solve the issue like that
"rootDir": "src",
"testRegex": ".spec.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
AFAIK Nest doesn’t add jest.config file, so package.json is my best bet.
But after doing this, my code still fails:
yarn run v1.22.5
$ jest
FAIL src/auth/auth.service.spec.ts
● Test suite failed to run
Cannot find module 'src/auth/auth.service' from 'auth/auth.service.spec.ts'
3 | import * as moment from 'moment';
4 | import { RedisService } from '@custom/redis-client'; // this is in node_modules
> 5 | import { AuthService } from 'src/auth/auth.service';
| ^
6 | import { ConfigService } from 'src/config/config.service';
7 |
8 | describe('AuthService', () => {
at Resolver.resolveModule (../node_modules/jest-resolve/build/index.js:306:11)
at Object.<anonymous> (auth/auth.service.spec.ts:5:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.486 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I also tried adding this to the jest
config, as well as tsconfig:
"moduleNameMapper": {
"src/(.*)": "<rootDir>/$1",
"tests/(.*)": "<rootDir>/__tests__/$1"
},
But with this code I get:
yarn run v1.22.5
$ jest
FAIL src/auth/auth.service.spec.ts
● Test suite failed to run
Configuration error:
Could not locate module ./src/redis/redis.health.indicator mapped as:
/home/aironside/Documents/sygnum/dev-environment/api-layer/src/$1.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/src/(.*)/": "/home/aironside/Documents/sygnum/dev-environment/api-layer/src/$1"
},
"resolver": undefined
}
at createNoMappedModuleFoundError (../node_modules/jest-resolve/build/index.js:551:17)
at Object.<anonymous> (../node_modules/@sygnum/redis-client/index.ts:1:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 3.183 s
Ran all test suites.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
What am I missing?
My full tsconfig:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"rootDir": "./",
}
}
2
Answers
I use the ts-jest module for working with Jest in my Typescript projects. I use Jest for unit testing and E2E testing (at the moment) so I have different configuration files for each, with a common base module to not repeat settings.
My tests do not need to specify the src/ when importing files, just the normal paths under that. Also, this configuration has the plugins to support using the Typescript paths (@LIBRARY, @MODULE, etc.) in your source code.
jest-base.config.js
Then for unit tests, I use the following jest.config.js, with the package.json script option
jest --watchAll --config ./jest.config.js
jest.config.js
For End to End tests, I use the following jest-e2e.config.js, with the package.json script option
jest --watchAll --config ./jest-e2e.config.js
jest-e2e.config.js
I had the same issue.
Please change
"rootDir": "src"
to"rootDir": "./"
and add"modulePaths": ["<rootDir>"].
in yourpakage.json
Finally your
jest
object should look like this:This worked fine for me. I hope you too!!