I created a simple REST API using Mongdoose with Nestjs. I have in total 2 tests and they are failing. The test output:
FAIL src/app/auth/auth.service.spec.ts ● Test suite failed to run
Cannot find module '@shared/errors' from 'auth.service.ts'
5 |
6 | import { AppLogger } from '../logger/logger';
> 7 | import { Errors } from '@shared/errors';
| ^
8 | import { ILoginDto } from './dto/login.dto';
9 | import { ITokenDto } from './dto/auth.dto';
10 | import { IUser } from '@user/document/user.doc';
at Resolver.resolveModule (../../node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (auth/auth.service.ts:7:1)
FAIL src/app/user/user.service.spec.ts ● Test suite failed to run
Cannot find module '@shared/errors' from 'user.service.ts'
1 | import { BadRequestException, Injectable } from '@nestjs/common';
2 | import { InjectModel } from '@nestjs/mongoose';
> 3 | import { Errors } from '@shared/errors';
| ^
4 | import { createMultipleRandom } from '@shared/utils';
5 | import { Model } from 'mongoose';
6 | import { AppLogger } from '../logger/logger';
at Resolver.resolveModule (../../node_modules/jest-resolve/build/index.js:259:17)
at Object.<anonymous> (user/user.service.ts:3:1)
Test Suites: 2 failed, 2 total Tests: 0 total Snapshots: 0 total Time: 2.751s Ran all test suites.
tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"paths": {
"@app/*": ["src/app/*"],
"@auth/*": ["src/app/auth/*"],
"@config/*": ["config/*"],
"@logger/*": ["src/app/logger/*"],
"@shared/*": ["src/app/shared/*"],
"@user/*": ["src/app/user/*"],
}
},
"include": [
"src/**/*"
],
"exclude": ["node_modules", "dist"]
}
auth.service.spec.ts:
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
describe('AuthService', () => {
let service: AuthService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [AuthService],
}).compile();
service = module.get<AuthService>(AuthService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});
In @shared/errors.ts I just export a constant variable. Since it is not a module, how should I import this in a test ? How can I solve this problem ?
If you're coming from google then this can also happen if VSCode autocompletes your imports as src/xyz/abc/service.ts instead of ../xyz/abc/service.ts.
When running tests the test code won't be able to locate the correct service(s) at run-time. It needs fully relative paths without an absolute 'src/' at the beginning.
As you are using typescript's path mapping, you need to also update your jest config with the mapped paths as well. Your jest config needs to have the following added:
{
...
"moduleNameMapper": {
"^@Shared/(.)*$": "<rootDir>/src/app/shared/$1"
}
}
Assuming that your <rootDir> is set to be . and not ./src. You can find more info on it here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With