Angular 示意图:在库中进行 运行 单元测试时出错
Angular Schematics: Error when running unit test in library
这是我第一次在原理图领域工作,所以很可能我只是没有正确设置。但我一直在关注 angular.io 上的指南和一些其他博客演练。我想将 ng-add 和 ng-update 原理图打包到一个组件库中,我已经设置好了 运行。在为 ng-add 设置初始管道后,我编写了一个简单的单元测试来验证设置,一旦它调用了新的 SchematicTestRunner,它就会爆炸并出现大量模块未找到错误,包括 child_process 和飞秒。事实上,除了创建测试运行器之外,我在单元测试中注释掉了所有内容,只是为了确定断点。除了按原样返回树之外,原理图本身还没有做任何事情。有趣的是,schematics-cli 生成的“空白”/sample schematics 项目根本没有使用 ng test,而是直接使用了 jasmine。但由于这是在现有的 Angular 库中,具有完整的测试套件,我想以相同的方式进行测试。如有任何帮助,我们将不胜感激!
错误部分:
ERROR in C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/tasks/node-package/executor.js
Module not found: Error: Can't resolve 'child_process' in 'C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package'
resolve 'child_process' in 'C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package'
Parsed request is a module
using description file: C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\package.json (relative path: ./tasks/node-package)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
looking for modules in C:/Users/<username>/src/fabric/ui
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: .)
Field 'browser' doesn't contain a valid alias configuration
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: ./child_process)
no extension
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.js doesn't exist
as directory
C:\Users\<username>\src\fabric\ui\child_process doesn't exist
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\ui\node_modules\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\node_modules doesn't exist or is not a directory
C:\Users\<username>\node_modules doesn't exist or is not a directory
C:\Users\node_modules doesn't exist or is not a directory
C:\node_modules doesn't exist or is not a directory
looking for modules in C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules
using description file: C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\package.json (relative path: ./node_modules)
Field 'browser' doesn't contain a valid alias configuration
looking for modules in C:\Users\<username>\src\fabric\ui\node_modules
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: ./node_modules)
Field 'browser' doesn't contain a valid alias configuration
using description file: C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\package.json (relative path: ./node_modules/child_process)
no extension
Field 'browser' doesn't contain a valid alias configuration
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: ./node_modules/child_process)
no extension
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.js doesn't exist
as directory
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process doesn't exist
C:\Users\<username>\src\fabric\ui\node_modules\child_process.js doesn't exist
as directory
C:\Users\<username>\src\fabric\ui\node_modules\child_process doesn't exist
[C:\Users\<username>\src\fabric\ui\child_process]
[C:\Users\<username>\src\fabric\ui\child_process.ts]
[C:\Users\<username>\src\fabric\ui\child_process.tsx]
[C:\Users\<username>\src\fabric\ui\child_process.mjs]
[C:\Users\<username>\src\fabric\ui\child_process.js]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\node_modules]
[C:\Users\<username>\src\fabric\node_modules]
[C:\Users\<username>\src\node_modules]
[C:\Users\<username>\node_modules]
[C:\Users\node_modules]
[C:\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.ts]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.ts]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.tsx]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.tsx]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.mjs]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.mjs]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.js]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.js]
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/tasks/node-package/executor.js 11:24-48
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/tasks/node/index.js
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/testing/schematic-test-runner.js
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/testing/index.js
@ ./schematics/ng-add/index.spec.ts
@ . sync \.spec\.ts$
@ ./test.ts
终于明白了运行宁! @mfaith 和我就 GitHub 与 Angular CLI 团队进行了对话。您可以在这里找到它:https://github.com/angular/angular-cli/issues/17986
有两个重要的要点:
- 您无法 运行 使用
ng
命令进行测试、构建和捆绑。所以 ng test
永远不会与原理图测试一起使用,因为它应该在浏览器中使用(与原理图不同)。
- 有一个虚拟项目可以作为参考。简单地 运行
schematics schematic your-name-for-this-project
它将创建一个包含 3 个基本原理图的原理图项目(名为 your-name-for-this-project)。那里有可用于 运行 在 library/app. 中执行原理图测试的脚本
我最后做了以下事情:
- 我在我的库文件夹中创建了一个文件
tsconfig.schematics.spec.json
(见下文)。
- 我在我的项目中添加了两个脚本 package.json(见下文)。
- 通过库文件夹中的
npm run schm:test
执行脚本。
我想,最初我这边有两个错误。首先,我想让原理图测试与 ng test
一起使用。其次,我可以使用 schematics schematic
项目中的配置文件解决配置问题。
tsconfig.schematics.spec.json:
{
"compilerOptions": {
"baseUrl": "tsconfig",
"lib": [
"es2018",
"dom"
],
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"rootDir": "schematics/",
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"sourceMap": true,
"strictNullChecks": true,
"target": "es6",
"types": [
"jasmine",
"node"
]
},
"include": [
"schematics/**/*"
],
"exclude": [
"schematics/*/files/**/*"
]
}
package.json 中的 2 个脚本:
"schm:build": "tsc -p tsconfig.schematics.spec.json",
"schm:test": "npm run schm:build && jasmine schematics/**/*.spec.js"
这是我第一次在原理图领域工作,所以很可能我只是没有正确设置。但我一直在关注 angular.io 上的指南和一些其他博客演练。我想将 ng-add 和 ng-update 原理图打包到一个组件库中,我已经设置好了 运行。在为 ng-add 设置初始管道后,我编写了一个简单的单元测试来验证设置,一旦它调用了新的 SchematicTestRunner,它就会爆炸并出现大量模块未找到错误,包括 child_process 和飞秒。事实上,除了创建测试运行器之外,我在单元测试中注释掉了所有内容,只是为了确定断点。除了按原样返回树之外,原理图本身还没有做任何事情。有趣的是,schematics-cli 生成的“空白”/sample schematics 项目根本没有使用 ng test,而是直接使用了 jasmine。但由于这是在现有的 Angular 库中,具有完整的测试套件,我想以相同的方式进行测试。如有任何帮助,我们将不胜感激!
错误部分:
ERROR in C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/tasks/node-package/executor.js
Module not found: Error: Can't resolve 'child_process' in 'C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package'
resolve 'child_process' in 'C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package'
Parsed request is a module
using description file: C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\package.json (relative path: ./tasks/node-package)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
looking for modules in C:/Users/<username>/src/fabric/ui
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: .)
Field 'browser' doesn't contain a valid alias configuration
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: ./child_process)
no extension
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\child_process.js doesn't exist
as directory
C:\Users\<username>\src\fabric\ui\child_process doesn't exist
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\ui\node_modules\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\fabric\node_modules doesn't exist or is not a directory
C:\Users\<username>\src\node_modules doesn't exist or is not a directory
C:\Users\<username>\node_modules doesn't exist or is not a directory
C:\Users\node_modules doesn't exist or is not a directory
C:\node_modules doesn't exist or is not a directory
looking for modules in C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules
using description file: C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\package.json (relative path: ./node_modules)
Field 'browser' doesn't contain a valid alias configuration
looking for modules in C:\Users\<username>\src\fabric\ui\node_modules
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: ./node_modules)
Field 'browser' doesn't contain a valid alias configuration
using description file: C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\package.json (relative path: ./node_modules/child_process)
no extension
Field 'browser' doesn't contain a valid alias configuration
using description file: C:\Users\<username>\src\fabric\ui\package.json (relative path: ./node_modules/child_process)
no extension
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process doesn't exist
.ts
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process.tsx doesn't exist
.mjs
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\child_process.mjs doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.js doesn't exist
as directory
C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process doesn't exist
C:\Users\<username>\src\fabric\ui\node_modules\child_process.js doesn't exist
as directory
C:\Users\<username>\src\fabric\ui\node_modules\child_process doesn't exist
[C:\Users\<username>\src\fabric\ui\child_process]
[C:\Users\<username>\src\fabric\ui\child_process.ts]
[C:\Users\<username>\src\fabric\ui\child_process.tsx]
[C:\Users\<username>\src\fabric\ui\child_process.mjs]
[C:\Users\<username>\src\fabric\ui\child_process.js]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node-package\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\tasks\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\node_modules]
[C:\Users\<username>\src\fabric\node_modules]
[C:\Users\<username>\src\node_modules]
[C:\Users\<username>\node_modules]
[C:\Users\node_modules]
[C:\node_modules]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.ts]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.ts]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.tsx]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.tsx]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.mjs]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.mjs]
[C:\Users\<username>\src\fabric\ui\node_modules\@angular-devkit\schematics\node_modules\child_process.js]
[C:\Users\<username>\src\fabric\ui\node_modules\child_process.js]
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/tasks/node-package/executor.js 11:24-48
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/tasks/node/index.js
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/testing/schematic-test-runner.js
@ C:/Users/<username>/src/fabric/ui/node_modules/@angular-devkit/schematics/testing/index.js
@ ./schematics/ng-add/index.spec.ts
@ . sync \.spec\.ts$
@ ./test.ts
终于明白了运行宁! @mfaith 和我就 GitHub 与 Angular CLI 团队进行了对话。您可以在这里找到它:https://github.com/angular/angular-cli/issues/17986
有两个重要的要点:
- 您无法 运行 使用
ng
命令进行测试、构建和捆绑。所以ng test
永远不会与原理图测试一起使用,因为它应该在浏览器中使用(与原理图不同)。 - 有一个虚拟项目可以作为参考。简单地 运行
schematics schematic your-name-for-this-project
它将创建一个包含 3 个基本原理图的原理图项目(名为 your-name-for-this-project)。那里有可用于 运行 在 library/app. 中执行原理图测试的脚本
我最后做了以下事情:
- 我在我的库文件夹中创建了一个文件
tsconfig.schematics.spec.json
(见下文)。 - 我在我的项目中添加了两个脚本 package.json(见下文)。
- 通过库文件夹中的
npm run schm:test
执行脚本。
我想,最初我这边有两个错误。首先,我想让原理图测试与 ng test
一起使用。其次,我可以使用 schematics schematic
项目中的配置文件解决配置问题。
tsconfig.schematics.spec.json:
{
"compilerOptions": {
"baseUrl": "tsconfig",
"lib": [
"es2018",
"dom"
],
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitThis": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"rootDir": "schematics/",
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"sourceMap": true,
"strictNullChecks": true,
"target": "es6",
"types": [
"jasmine",
"node"
]
},
"include": [
"schematics/**/*"
],
"exclude": [
"schematics/*/files/**/*"
]
}
package.json 中的 2 个脚本:
"schm:build": "tsc -p tsconfig.schematics.spec.json",
"schm:test": "npm run schm:build && jasmine schematics/**/*.spec.js"