找不到Nestjs测试模块



我使用带有Nestjs的Mongdoose创建了一个简单的REST API。我总共有两次测试,但都不及格。测试输出:

失败src/app/auth/auth.service.spec.ts●测试套件无法运行

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)

失败src/app/user/user.service.spec.ts●测试套件无法运行

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)

测试套件:2个失败,共2个测试:共0个快照:共0个时间:2.751秒运行所有测试套件。

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();
});
});

在@shared/errors.ts中,我只是导出一个常量变量。既然它不是一个模块,我应该如何在测试中导入它?我该如何解决这个问题?

如果你来自谷歌,那么如果VSCode自动将你的导入完成为src/xyz/abc/service.ts而不是../xyz/abc/service.ts,也可能发生这种情况。

当运行测试时,测试代码将无法在运行时找到正确的服务。它需要完全相对的路径,而在一开始没有绝对的'src/'

在使用typescript的路径映射时,还需要使用映射的路径更新jest配置。您的笑话配置需要添加以下内容:

{
...
"moduleNameMapper": {
"^@Shared/(.)*$": "<rootDir>/src/app/shared/$1"
}
}

假设您的<rootDir>设置为.而不是./src。你可以在这里找到更多信息。

以下是jest路径解决问题的详细配置。

moduleNameMapper中定义的键将替换为它们的值。

例如,当jest看到"@resources"时,它会将其替换为"src/resources">

(使用索引文件帮助很大(

包.json 中的Jest配置

"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"moduleNameMapper": {
"@resources": "<rootDir>/resources",
"@shared": "<rootDir>/shared",
"@email": "<rootDir>/email",
"@auth": "<rootDir>/auth",
"@config": "<rootDir>/config",
"@database": "<rootDir>/database"
},
"testRegex": ".*\.spec\.ts$",
"transform": {
"^.+\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}

Typescript配置

"paths": {
"@resources/*": ["src/resources/*"],
"@resources": ["src/resources"],
"@shared/*": ["src/shared/*"],
"@shared": ["src/shared"],
"@email/*": ["src/email/*"],
"@email": ["src/email"]
}

最新更新