我有一个 NX 工作区,里面有 Angular 和 NestJS。我也有一些共享库。我想测试一个使用生成的共享库之一的 NestJS API 项目
nx g @nrwl/nest:lib shared/backend
我只是在我的 NestJS 项目中使用它,就像对 NX 分配的名称进行正常导入一样。当我在开发模式下运行项目本身时效果很好,但是当我使用 @nestjs/testing 模块运行应用程序时,它会失败。
Error: Cannot find module '@proj/shared/backend'
Require stack:
- C:\Users\ebaamic\Projects\proj\apps\proj-api\src\app\proj\proj.controller.ts
- C:\Users\ebaamic\Projects\proj\apps\proj-api\src\app\proj\proj.module.ts
- C:\Users\ebaamic\Projects\proj\apps\proj-api\src\app\app.module.ts
- C:\Users\ebaamic\Projects\proj\apps\proj-api-e2e\step-definitions\step-definition.base.ts
- C:\Users\ebaamic\Projects\proj\node_modules\@cucumber\cucumber\lib\cli\index.js
- C:\Users\ebaamic\Projects\proj\node_modules\@cucumber\cucumber\lib\cli\run.js
- C:\Users\ebaamic\Projects\proj\node_modules\@cucumber\cucumber\bin\cucumber-js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:880:15)
at Function.Module._load (internal/modules/cjs/loader.js:725:27)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\ebaamic\Projects\proj\apps\proj-api\src\app\proj\proj.controller.ts:22:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (C:\Users\ebaamic\Projects\proj\node_modules\ts-node\src\index.ts:1310:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Object.require.extensions.<computed> [as .ts] (C:\Users\ebaamic\Projects\proj\node_modules\ts-node\src\index.ts:1313:12)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Module.require (internal/modules/cjs/loader.js:952:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.<anonymous> (C:\Users\ebaamic\Projects\proj\apps\proj-api\src\app\proj\proj.module.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Module.m._compile (C:\Users\ebaamic\Projects\proj\node_modules\ts-node\src\index.ts:1310:23)
codepath: C:\Users\ebaamic\Projects\proj\apps\proj-api-e2e\step-definitions\step-definition.base.ts
从我的 API 代码中删除库引用时,我的测试运行良好,所以它实际上只在黄瓜测试中。这是我创建测试模块的方式:
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
}).compile();
this.context.app = moduleFixture
.createNestApplication()
.useGlobalPipes(new ValidationPipe());
await this.context.app.init();
要启动 e2e 测试,我调用以下命令:
cucumber-js -p proj-api
我的 tsconfig.json 看起来像这样:
{
"compileOnSave": false,
"compilerOptions": {
"rootDir": ".",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
"lib": ["es2017", "dom"],
"skipLibCheck": true,
"skipDefaultLibCheck": true,
"baseUrl": ".",
"paths": {
"@proj/shared/backend": [
"libs/shared/backend/src/index.ts"
],
"@proj/shared/client": [
"libs/shared/client/src/index.ts"
],
"@proj/shared/common": [
"libs/shared/common/src/index.ts"
],
"@proj/shared/eds": [
"libs/shared/eds/src/index.ts"
],
"@proj/shared/feature": [
"libs/shared/feature/src/index.ts"
]
}
},
"exclude": ["node_modules", "tmp"]
}
我在根目录下的 cucumber.js
module.exports = {
'proj-api': [
'apps/proj-api-e2e/features/**/*.feature', // Specify our feature files
'--require-module ts-node/register', // Load TypeScript module
'--require apps/proj-api-e2e/step-definitions/**/*.ts', // Load step definitions
'--format progress-bar', // Load custom formatter
'--publish-quiet',
'--parallel 0',
'--exit',
].join(' '),
};
这是我在 package.json 中的依赖项
"dependencies": {
"@angular-architects/module-federation": "^12.2.0",
"@angular/animations": "^12.2.0",
"@angular/cdk": "^12.2.8",
"@angular/common": "^12.2.0",
"@angular/compiler": "^12.2.0",
"@angular/core": "^12.2.0",
"@angular/forms": "^12.2.0",
"@angular/material": "^12.2.8",
"@angular/platform-browser": "^12.2.0",
"@angular/platform-browser-dynamic": "^12.2.0",
"@angular/router": "^12.2.0",
"@nestjs/axios": "0.0.2",
"@nestjs/common": "^8.0.0",
"@nestjs/config": "^1.0.2",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/swagger": "^5.1.0",
"cache-manager": "^3.4.4",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"d3": "^5.16.0",
"dragula": "^3.7.3",
"leaflet": "^1.7.1",
"mapbox-gl": "^2.3.0",
"moment": "^2.29.1",
"mysql": "^2.18.1",
"normalize.css": "^8.0.1",
"reflect-metadata": "^0.1.13",
"rxjs": "~6.6.0",
"stream": "0.0.2",
"swagger-ui-express": "^4.1.6",
"tslib": "^2.0.0",
"uuid": "^8.3.2",
"zone.js": "~0.11.4"
},
"devDependencies": {
"@angular-devkit/build-angular": "~12.2.0",
"@angular-eslint/eslint-plugin": "~12.3.0",
"@angular-eslint/eslint-plugin-template": "~12.3.0",
"@angular-eslint/template-parser": "~12.3.0",
"@angular/compiler-cli": "^12.2.0",
"@angular/language-service": "^12.2.0",
"@cucumber/cucumber": "^7.3.1",
"@cypress/code-coverage": "^3.9.11",
"@nestjs/schematics": "^7.0.0",
"@nestjs/testing": "^8.0.8",
"@nrwl/angular": "^12.9.0",
"@nrwl/cli": "12.9.0",
"@nrwl/cypress": "12.9.0",
"@nrwl/eslint-plugin-nx": "12.9.0",
"@nrwl/jest": "12.9.0",
"@nrwl/linter": "12.9.0",
"@nrwl/nest": "^12.9.0",
"@nrwl/node": "12.9.0",
"@nrwl/tao": "12.9.0",
"@nrwl/workspace": "12.9.0",
"@types/jest": "26.0.24",
"@types/node": "14.14.33",
"@typescript-eslint/eslint-plugin": "~4.28.3",
"@typescript-eslint/parser": "~4.28.3",
"chai": "^4.3.4",
"cross-env": "^7.0.3",
"cucumber-pretty": "^6.0.0",
"cucumber-tsflow": "^4.0.0-preview.7",
"cypress": "^8.3.0",
"cypress-cucumber-preprocessor": "^4.2.0",
"cypress-fail-on-console-error": "^2.1.2",
"deep-object-diff": "^1.1.0",
"eslint": "7.22.0",
"eslint-config-prettier": "8.1.0",
"eslint-plugin-cypress": "^2.10.3",
"jest": "27.0.3",
"jest-preset-angular": "9.0.7",
"prettier": "^2.3.1",
"rimraf": "^3.0.2",
"sonarqube-scanner": "^2.8.1",
"supertest": "^6.1.3",
"testcafe": "^1.16.0",
"ts-jest": "27.0.3",
"ts-node": "^10.2.1",
"typeorm": "^0.2.37",
"typeorm-extension": "^0.3.0",
"typescript": "~4.3.5"
},
看起来 cucumber-js 不使用定义库的 tsconfig ,因为当我正常运行代码时它可以工作,而当我删除 lib 导入时,我的测试也可以工作。
有人知道吗?