Angular 9 + CLI (TypeScript) - 如何停止生成 .spec.ts 测试文件 [英] Angular 9 + CLI (TypeScript) - How to stop generating .spec.ts test files
问题描述
我知道这是一种不好的做法,但请耐心等待:
我正在使用 Angular-CLI,特别是 ng g
来生成我的所有类.但是,我对任何 *.spec.ts
测试文件不感兴趣.我知道有两个标志(--inline-template
、--inline-style
)来处理内联 CSS 和 HTML 而不是分离的文件,并且对于规范 --spec
标志默认设置为 true.
I'm using Angular-CLI, particularly ng g
to generate all of my classes. However, I'm not interested in any *.spec.ts
test files. I know that there are two flags (--inline-template
, --inline-style
) to handle inline CSS and HTML instead of separated files, and for spec the --spec
flag is set to true by default.
所以对于每次运行,是的,我可以执行 ng g c foo --it --is --spec=false
.
So for each run, yes, I could do ng g c foo --it --is --spec=false
.
但是如何在全局范围内禁用测试文件的创建?有没有默认设置?
But how do I disable the creation of test files globally? Is there any default setting for it?
草率地,我做了一些(没用的),比如:
Rashly, I did some stuff (that didn't work), like:
ng set spec=false --global
我还尝试通过填充排除数组来配置我的 src/tsconfig.json
:
I also tried configuring my src/tsconfig.json
by filling the exclude array:
"exclude": [
"**/*.spec.ts"
]
推荐答案
For Angular 9 > (version 6 > below)
1) 将代码片段复制到 angular.json
的根目录,(为所有项目/全局配置设置).
2) 或将代码片段复制到 angular.json
中特定项目的根目录 (projects.your-project-name
)(为特定项目配置设置).
For Angular 9 > (version 6 > below)
1) Copy snippet to the root of angular.json
, (configures settings to all projects/globally).
2) Or copy snippet to the root of a specific project (projects.your-project-name
) in angular.json
(configures settings for a specific project).
"schematics": {
"@schematics/angular:component": {
"style": "scss",
"skipTests": true
},
"@schematics/angular:class": {
"skipTests": true
},
"@schematics/angular:directive": {
"skipTests": true
},
"@schematics/angular:pipe": {
"skipTests": true
},
"@schematics/angular:service": {
"skipTests": true
}
},
每种文件类型的所有可配置选项 示意图选项:
"schematics": {
"@schematics/angular:component": {
"changeDetection": "Default",
"entryComponent": false,
"export": false,
"flat": false,
"inlineStyle": false,
"inlineTemplate": false,
"module": "",
"prefix": "",
"selector": "",
"skipImport": false,
"spec": true,
"style": "css",
"viewEncapsulation": "Emulated",
"skipTests": "false"
},
"@schematics/angular:module": {
"commonModule": true,
"flat": false,
"module": "",
"routing": false,
"routingScope": "Child"
},
"@schematics/angular:service": {
"flat": true,
"skipTests": true
},
"@schematics/angular:pipe": {
"export": false,
"flat": true,
"module": "",
"skipImport": false,
"skipTests": true
},
"@schematics/angular:directive": {
"export": false,
"flat": true,
"module": "",
"prefix": "app",
"selector": "",
"skipImport": false,
"skipTests": true
},
"@schematics/angular:class": {
"skipTests": true
}
},
对于 Angular 6 >
1) 将代码片段复制到 angular.json
的根目录,(为所有项目/全局配置设置).
2) 或将代码片段复制到 angular.json
中特定项目的根目录 (projects.your-project-name
)(为特定项目配置设置).
For Angular 6 >
1) Copy snippet to the root of angular.json
, (configures settings to all projects/globally).
2) Or copy snippet to the root of a specific project (projects.your-project-name
) in angular.json
(configures settings for a specific project).
"schematics": {
"@schematics/angular:component": {
"styleext": "scss",
"spec": false
},
"@schematics/angular:class": {
"spec": false
},
"@schematics/angular:directive": {
"spec": false
},
"@schematics/angular:guard": {
"spec": false
},
"@schematics/angular:module": {
"spec": false
},
"@schematics/angular:pipe": {
"spec": false
},
"@schematics/angular:service": {
"spec": false
}
},
每种文件类型的所有可配置选项 (原理图选项):
All configurable options per type of file (Schematic Options):
"schematics": {
"@schematics/angular:component": {
"changeDetection": "Default",
"export": false,
"flat": false,
"inlineStyle": false,
"inlineTemplate": false,
"module": "",
"prefix": "",
"selector": "",
"skipImport": false,
"spec": true,
"styleext": "css",
"viewEncapsulation": "Emulated"
},
"@schematics/angular:module": {
"commonModule": true,
"flat": false,
"module": "",
"routing": false,
"routingScope": "Child",
"spec": true
},
"@schematics/angular:service": {
"flat": true,
"spec": true
},
"@schematics/angular:pipe": {
"export": false,
"flat": true,
"module": "",
"skipImport": false,
"spec": true
},
"@schematics/angular:directive": {
"export": false,
"flat": true,
"module": "",
"prefix": "app",
"selector": "",
"skipImport": false,
"spec": true
},
"@schematics/angular:class": {
"spec": true
}
},
<小时>
使用 Angular CLI 配置 Angular CLI
错误:
ng set defaults.spec.component false
命令导致错误:get/set has been deprecated for the config 命令.
ng set
改为 ng config
.
使用 Angular CLI(配置命令用法):
用于在 angular.json
中生成规范、内联模板、内联样式等的设置现在保留在 schematics.@schematics/angular.<file-type>.<;设置>
.
The settings for generating specs, inline templates, inline styling etc. within angular.json
are now persisted inside the schematics.@schematics/angular.<file-type>.<setting>
.
运行 ng config schemas.@schematics/angular.component.spec false
来配置组件的规范.此命令在 angular.json
文件中的原理图属性中添加设置.
Run ng config schematics.@schematics/angular.component.spec false
to configure spec for components. This command adds the setting inside the schematics property within the angular.json
file.
Angular CLI 工作区文件(angular.json) 在 Angular Github 上
这篇关于Angular 9 + CLI (TypeScript) - 如何停止生成 .spec.ts 测试文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!