运行测试时,玩笑测试给我“找不到模块”,但代码在测试之外编译得很好。这是怎么回事?

plu*_*ium 1 jestjs nestjs

我正在使用 NestJS 和 TypeScript 为大型应用程序编写第一个测试。我想导入一个实体,该实体从另一个文件夹中的模块导入接口。事实证明,这具有挑战性。

该项目的结构如下

apps
..apis
--..web-api
----..src
----..package.json
----..jest.config.js
------..app
--------..profiles
----------.._entities
------------..profile.entity.spec.ts <--- the test
------------..profile.entity.ts


libs
--app-interfaces
----src
------lib
--------profile
----------index.ts <--- the files the test is trying to import and test 
----------gender.enum.ts
----------profile.interface.ts
Run Code Online (Sandbox Code Playgroud)

在 profile.entity.spec.ts 中我有:

apps
..apis
--..web-api
----..src
----..package.json
----..jest.config.js
------..app
--------..profiles
----------.._entities
------------..profile.entity.spec.ts <--- the test
------------..profile.entity.ts


libs
--app-interfaces
----src
------lib
--------profile
----------index.ts <--- the files the test is trying to import and test 
----------gender.enum.ts
----------profile.interface.ts
Run Code Online (Sandbox Code Playgroud)

在 profile.entity.ts 中:

import { ProfileEntity } from "./profile.entity";
import { GenderEnum, ProfileTypeEnum } from '../../../../../../../libs/app-interfaces/src/lib/profile';
// all that ../../ is what I have to do to get to the import and have it recognized in this .spec file
// it doesn't recognize the @ symbol taking us to the root of the project

describe('Profile class', () => {
    it('should make a profile with no fields', () => {
      const profile = new ProfileEntity();
      expect(profile).toBeTruthy();
    });
  });

Run Code Online (Sandbox Code Playgroud)

当应用程序由 Docker 编译时,profile.entity.ts 文件可以正常工作。但是,当我在 Jest 中运行 profile.entity.spec.ts 文件时,我得到:

Cannot find module '@ourProject/app-interfaces' from 'src/app/profiles/_entities/profile.entity.ts' // <--- this is a big clue

    Require stack:
      src/app/profiles/_entities/profile.entity.ts
      src/app/profiles/_entities/profile.entity.spec.ts

      1 | import { Column, CreateDateColumn, DeleteDateColumn, Entity, PrimaryColumn, UpdateDateColumn } from 'typeorm';
    > 2 | import { GenderEnum, IProfile, ProfileTypeEnum } from '@ourProject/app-interfaces';
        | ^
      3 |
      4 |
      5 | @Entity({ name: 'profile' })

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
      at Object.<anonymous> (src/app/profiles/_entities/profile.entity.ts:2:1)
Run Code Online (Sandbox Code Playgroud)

我需要在 jest.config.js 中使用一些特殊配置来让 jest 测试访问 package.json 顶部 lvl 之外的文件吗?

这是 jest.config.js

import { Column, CreateDateColumn, DeleteDateColumn, Entity, PrimaryColumn, UpdateDateColumn } from 'typeorm';
import { GenderEnum, IProfile, ProfileTypeEnum } from '@ourProject/app-interfaces';


@Entity({ name: 'profile' })
export class ProfileEntity implements IProfile {
    @PrimaryColumn({ nullable: false, unique: true })
    id: string;
Run Code Online (Sandbox Code Playgroud)

Cannot find module '@ourProject/app-interfaces' from 'src/app/profiles/_entities/profile.entity.ts'文件正常运行时不会出现该行。

小智 7

假设您的tsconfig.json别名配置如下所示:

"paths": {
    "@ourProject/*": ["./libs/*"],
}
Run Code Online (Sandbox Code Playgroud)

然后你需要添加moduleNameMapper一行jest.config.js

{
    moduleNameMapper: {
        '^@ourProject/(.*)$': ['<rootDir>/libs/$1'],
    },
}

Run Code Online (Sandbox Code Playgroud)

我没有遇到别名导入位于不同文件夹中的问题,但 jest 无法识别tsconfig.json.