1

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

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

有谁知道可能会发生什么?谢谢!

4

1 回答 1

3

我已将路径更改为:“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"
  }
}

问候!

于 2020-04-22T09:32:57.163 回答