jestjs - 找不到 Nestjs 测试模块

标签 jestjs nestjs

我使用 Mongdoose 和 Nestjs 创建了一个简单的 REST API。我总共进行了 2 次测试,但都失败了。
测试输出:

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)

失败 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.751s
运行所有测试套件。

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 我只是导出一个常量变量。既然它不是一个模块,我应该如何在测试中导入它?我怎么解决这个问题 ?

最佳答案

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

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

假设您的 <rootDir>设置为 .而不是 ./src .您可以find more info on it here .

关于jestjs - 找不到 Nestjs 测试模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60616994/

相关文章:

javascript - 使用最新版本的 d3-path 配置 jest

javascript - 开 Jest 期望之后的一切都没有被调用

nestjs - TypeORM 问题保存实体级联 true

node.js - 用于特定 Controller 路由的 NestJS CSRF

javascript - 如何使用 jest 模拟从方法内部启动的构造函数

javascript - 模拟子组件元素在父组件测试文件中的单击

jestjs - 使用 jest.runCLI() 运行单个测试

javascript - NestJS 根据浏览器语言传递静态文件

javascript - 如何在nestjs中转换api响应?

mysql - TypeORM delete using WHERE with OR operator using Repository