当我尝试使用NestJS,Jest和GraphQLFederationModule测试时出错

时间:2020-04-22 03:15:18

标签: jestjs apollo nestjs ts-jest protobufjs

当我尝试使用此模块进行测试时,会出现以下错误:

 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)

最好的配置文件是:

{
  "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"
  }
}

app.module文件:

@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");

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

1 个答案:

答案 0 :(得分:0)

我已将路径的形式从“ src / folder / ...”更改为“ @ App / folder / ...”,最后效果很好,但我很好奇为什么会发生这种情况,如果有任何解决方案,我会尽力提供可能的解决方案和建议,非常感谢!

如果有人想查看解决方案,如下所示:

tsconfig.json:

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

最有趣的配置文件:

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

致谢!