尝试使用NestJS、Jest和GraphQLFederationModule进行测试时出错

zdwk9cvp  于 2022-12-25  发布在  Jest
关注(0)|答案(2)|浏览(190)

当我尝试使用此模块进行测试时,它给我以下错误:

FAIL  test/test.e2e-spec.ts (28.561s)
  AppController (e2e)
    × test (4555ms)

  ● AppController (e2e) › test

    Configuration error:

    Could not locate module ./src/index-minimal mapped as:
    C:\absolute\path\server\company\src\$1.

    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/src\/(.*)/": "C:\absolute\path\server\company\src\$1"
      },
      "resolver": undefined
    }

      at createNoMappedModuleFoundError (../node_modules/jest-resolve/build/index.js:545:17)
      at Object.<anonymous> (../node_modules/@apollo/protobufjs/minimal.js:4:18)

jest配置文件为:

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "testEnvironment": "node",
  "moduleDirectories": ["node_modules", "src"],
  "moduleNameMapper": {
    "src/(.*)": "<rootDir>/../src/$1"
  }
}

应用程序模块文件:

@Module({
  imports: [
    ...,
    GraphQLFederationModule.forRootAsync({
      imports: [CustomConfigModule],
      inject: [EnvironmentStateService],
      useFactory: (environmentStateService: EnvironmentStateService) => ({
        introspection: environmentStateService.isDev,
        debug: environmentStateService.isDev,
        playground: environmentStateService.isDev,
        autoSchemaFile: true,
        tracing: environmentStateService.isDev,
      }),
    }),
    ...,
  ],
})
export class AppModule {}

当我在app.module中尝试以下设置时,它工作正常:

@Module({
  imports: [
    ...,
    GraphQLModule.forRootAsync({
      imports: [CustomConfigModule],
      inject: [EnvironmentStateService],
      useFactory: (environmentStateService: EnvironmentStateService) => ({
        introspection: environmentStateService.isDev,
        debug: environmentStateService.isDev,
        playground: environmentStateService.isDev,
        autoSchemaFile: true,
        tracing: environmentStateService.isDev,
      }),
    }),
    ...,
  ],
})
export class AppModule {}

我一直在查看文件中出现错误的地方,我在node_modules(minimal.js)中发现了以下protobufjs代码:

// minimal library entry point.

"use strict";
module.exports = require("./src/index-minimal");

有人知道会发生什么吗?谢谢!

ecbunoof

ecbunoof1#

我已经改变了我的道路的形式:“src/folder/...”到“@App/folder/...”,最后它工作正常,但我很好奇为什么会发生这种情况,如果有任何解决方案,我会留意可能的解决方案和建议,非常感谢!
如果有人想看看解决方案,它是如下:
tsconfig.json文件夹:

{
  "compilerOptions": {
    ...
    "baseUrl": "./",
    "paths": {
      "@App/*": ["src/*"]
    },
    ...
  },
  ...
}

jest配置文件:

{
  "moduleFileExtensions": ["js", "json", "ts"],
  "rootDir": ".",
  "testRegex": ".e2e-spec.ts$",
  "transform": {
    "^.+\\.(t|j)s$": "ts-jest"
  },
  "testEnvironment": "node",
  "moduleNameMapper": {
    "^@App/(.*)$": "<rootDir>/../src/$1"
  }
}

问候!

mzmfm0qo

mzmfm0qo2#

我遇到了同样的问题,我的jest-e2e.json文件也是类似的

"moduleNameMapper": {
    "src/(.*)": "<rootDir>/../src/$1"
}

发生错误Could not locate module ./src/index-minimal mapped as: ...是因为protobufjs中的minimal.js文件试图导入./src/index-minimal,但我们的Jest runner将文件路径名重新Map为<rootDir>/../src/$1,因此protobufjs抛出错误,因为它找不到./src/index-minimal.js文件。
解决方案是不去管src/index-minimal,这样我们就不用重新Map它的名称了。我在jest-e2e.json中的moduleNameMapper字段中添加了以下行,这样就解决了这个问题

"moduleNameMapper": {
    "src/index-minimal": "./src/index-minimal",
    "src/(.*)": "<rootDir>/../src/$1"
}

请注意,不要在src文件夹中创建名为index-minimal的文件,因为Jest将无法正确Map文件名。

相关问题