当我尝试使用此模块进行测试时,它给我以下错误:
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");
有人知道会发生什么吗?谢谢!
2条答案
按热度按时间ecbunoof1#
我已经改变了我的道路的形式:“src/folder/...”到“@App/folder/...”,最后它工作正常,但我很好奇为什么会发生这种情况,如果有任何解决方案,我会留意可能的解决方案和建议,非常感谢!
如果有人想看看解决方案,它是如下:
tsconfig.json文件夹:
jest配置文件:
问候!
mzmfm0qo2#
我遇到了同样的问题,我的
jest-e2e.json
文件也是类似的发生错误
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
字段中添加了以下行,这样就解决了这个问题请注意,不要在
src
文件夹中创建名为index-minimal
的文件,因为Jest将无法正确Map文件名。