每当我创建一个新组件时,都有一种方法可以摆脱Angular 2+中的spec.ts文件。我知道这是出于测试目的,但是如果我不需要它该怎么办。
可能有一些设置可以禁用此特定的测试文件。
已针对角度8 CLI更新
ng generate component --skipTests=true component-name
在angular-cli.json
内部,将spec.component参数设置为false
:
{
...
"defaults" : {
...
"spec": {
...
"component": false
}
}
}
或在创建过程中使用--spec=false
选项
ng generate component --spec=false component-name
对于Angular 6
ng config schematics.@schematics/angular.component.spec false
使用angular-cli时,有很多选择要传递。要生成没有测试的新组件,您可以像这样简单地添加--spec=false
:
ng generate component --spec=false my-component
[angular-cli.json中还有一个选项,您要默认启用其类型的类型,可以在其中修改行为:
"defaults": {
"spec": {
"class": false,
"component": true,
"directive": true,
"module": false,
"pipe": true,
"service": true
}
}
对于Angular 6 +只需运行以下命令:
ng config schematics.@schematics/angular.component.spec false
OR
只需将此添加到您的angular.json文件中,就可以了
"schematics": {
"@schematics/angular": {
"component": {
"spec": false
}
}
}
注意:在粘贴此检查是否有冲突之前,希望对您有所帮助
在最新的Angular版本中,您可以配置cli生成器,以禁止为angular-cli.json
中的组件和服务创建规范文件,如下所示:
"defaults": {
"component": { "spec": false },
"service": { "spec": false },
...
}
您可以添加额外的行来也禁用守卫和职业等等的规范。
对于Angular 7及更高版本,它将起作用:
ng generate component componentname --skipTests
或 ng generate component componentname --skipTests=true
只需尝试这个。这正在工作
ng g c component_name --spec false
默认情况下有一个禁用Angular生成“ spec”文件的命令
ng set defaults.class.spec=false
ng set defaults.component.spec=false
UPDATE:对于Angular 6
ng config schematics.@schematics/angular.component.spec false
使用Angular 7中的此简单命令简单地创建组件
ng g c component-name --spec false
或者如果真的不想将其包含在任何组件中,只需更新angular.json
"@schematics/angular": {
"component": {
"spec": false
}
}
对于Angular 8:ng generate component mycomponent --skipTests=false
ng g c component-name --spec false
根据fix #156,您可以使用以下方法创建没有规格文件的组件>
ng generate component my-comp --nospec
对于Angular 6,将其添加到"schematics":{}
中的angular.json中:
这里是角度9的含义
--spec
将不再起作用。请改用--skip-tests
。
创建组件时可以使用以下命令
如果要跳过spec.ts文件,请使用以下命令:
仅添加此信息,是因为我发现自己也遇到同样的问题,并且无法通过使用当前答案来解决它,而且我不想在angular-cli.json
文件上进行更改。似乎最新版本的Angular CLI --spec=false
已过时,因此不再起作用,请改用--skipTests
。
如果要跳过特定组件的规格文件。
对于Angular 9,您可以使用