使用 Angular 11 CLI 时,当我生成一个新组件时,我可以指定选项
--skip-tests
以避免在此阶段生成.component.spec.ts
。
ng generate component --name asdf --module app --skip-tests --dry-run
当我生成包含新组件(
--route
选项)的新模块时,我无法指定--skip-tests
选项。
ng generate module --name asdf --module app --route asdf --routing true --skip-tests --dry-run
如果我这样做,我会收到错误:
Unknown option: '--skip-tests'
在这种情况下是否有另一种方法可以跳过生成测试,或者只是不支持
generate module
?
根据本文,您可以在生成整个项目时添加
--skip-tests
标志,或者生成单个组件。从逻辑角度来看,您无法在模块上应用该标志是有道理的,因为模块不是用测试文件生成的。
要对所有将来生成的代码进行更改,请通过将
angular.json
添加到原理图部分来修改您的 skipTests:true
文件。
"schematics": {
"@schematics/angular:component": {
"style": "scss",
"skipTests": true
},
"@schematics/angular:class": {
"skipTests": true
},
"@schematics/angular:directive": {
"skipTests": true
},
"@schematics/angular:guard": {
"skipTests": true
},
"@schematics/angular:module": {
"skipTests": true
},
"@schematics/angular:pipe": {
"skipTests": true
},
"@schematics/angular:service": {
"skipTests": true
}
}
--skip-tests
不受 ng generate module
命令支持。
--flat
--lint-fix
--module
--project
--route
--routing
--routing-scope
更新:
另一种方法是依次使用两个命令:
ng generate module --name am && ng generate component --name ac --module am --skip-tests --dry-run
&&
表示只有在command1执行成功后才执行command2。
请注意,vscode 集成终端不支持 && ,可能的解决方法可以在这里
找到结果应该是这样的:
我使用 Angular CLI 版本 10,但 Angular CLI v11 应该会产生相同的结果。
使用以下命令跳过测试文件:ng g c name --skip-tests
我检查了 Angular CLI 发行说明。据我了解,版本 13.2.0-next.2 (2022-01-12) 进行了更新,从 --skipTest 更改为 --skip-tests。这意味着如果您使用的 Angular CLI 版本早于 13.2.0,则应使用 --skipTest。否则,您需要使用 --skip-tests 表达式。
欲了解更多信息,您可以查看以下链接