I'm trying to work with tests in NestJS now and I couldn't move forward due to a problem with the location of the modules.
The application was already responding, the problems started when I tried to run the tests.
The first try gave me several errors like this:
Cannot find module 'src/common/dto/paginated-response.dto' from 'applications/applications.service.ts'
According my reading of Tejas, I understood that the problem was the relative path starting in 'src/...'.
Then, I read the tutorial by Amir Mustafa for converting the relative paths to absolute paths from the configuration.
After replacing the relative paths with the @app
key, I got to run the application, but the tests failed again. This time, the errors were like this:
Cannot find module '@app/users/users.controller' from 'users/users.controller.spec.ts'
After reading the question/answer by dragons0458, I understood that I should to configure moduleNameMapper inside jest section of package.json. The section is so now:
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node",
"moduleNameMapper": {
"^@app/(.*)": "<rootDir>./dist/$1"
}
}
The third try gave me new errors, below, but now I don't know what I should do. It looks to me that $1 in moduleNameMapper is not converted to path.
Configuration error:
Could not locate module @app/gateways/gateways.controller mapped as:
[MY HOME]/backend_nestjs/src/dist/$1.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^@app\/(.*)/": "[MY HOME]/backend_nestjs/src/dist/$1"
},
"resolver": undefined
}
1 | import { Test, TestingModule } from '@nestjs/testing';
> 2 | import { GatewaysController } from '@app/gateways/gateways.controller';
| ^
3 | import { GatewaysService } from '@app/gateways/gateways.service';
4 |
5 | describe('GatewaysController', () => {
at createNoMappedModuleFoundError (../node_modules/jest-resolve/build/resolver.js:759:17)
at Object.<anonymous> (gateways/gateways.controller.spec.ts:2:1)
The application is answering in 3000 port, but the tests (12 tests) fail. What is lacking for the correct configuration for modules?
I am pasting here two files that I think can help to understand my configuration:
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": true,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"paths": {
"@app/*": ["./src/*"]
}
}
}
jest-e2e.json
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
}
}
Thank you very much.
Top comments (0)