简体   繁体   English

Angular 9 + CLI (TypeScript) - 如何停止生成.spec.ts 测试文件

[英]Angular 9 + CLI (TypeScript) - How to stop generating .spec.ts test files

I know it's kind of a bad practice , but bear with me:我知道这是一种不好的做法,但请耐心等待:

I'm using Angular-CLI, particularly ng g to generate all of my classes.我正在使用 Angular-CLI,尤其是ng g来生成我所有的类。 However, I'm not interested in any *.spec.ts test files.但是,我对任何*.spec.ts测试文件都不感兴趣。 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.我知道有两个标志( --inline-template--inline-style )来处理内联 CSS 和 HTML 而不是单独的文件,并且对于规范--spec标志默认设置为 true 。

So for each run, yes, I could do ng g c foo --it --is --spec=false .所以对于每次运行,是的,我可以执行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

I also tried configuring my src/tsconfig.json by filling the exclude array:我还尝试通过填充排除数组来配置我的src/tsconfig.json

"exclude": [
    "**/*.spec.ts"
]

For Angular 9 > (version 6 > below)对于 Angular 9 >(版本 6 > 以下)

1) Copy snippet to the root of angular.json , (configures settings to all projects/globally). 1) 将代码片段复制到angular.json的根目录,(配置所有项目/全局的设置)。
2) Or copy snippet to the root of a specific project ( projects.your-project-name ) in angular.json (configures settings for a specific project). 2)或者将代码片段复制到angular.json特定项目( projects.your-project-name )的根目录(为特定项目配置设置)。

"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
  }
},

All configurable options per type of file Schematic Options :每种文件类型的所有可配置选项示意图选项

"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
  }
},

For Angular 6 >对于 Angular 6 >

1) Copy snippet to the root of angular.json , (configures settings to all projects/globally). 1) 将代码片段复制到angular.json的根目录,(配置所有项目/全局的设置)。
2) Or copy snippet to the root of a specific project ( projects.your-project-name ) in angular.json (configures settings for a specific project). 2)或者将代码片段复制到angular.json特定项目( projects.your-project-name )的根目录(为特定项目配置设置)。

"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 configuration with Angular CLI带有 Angular CLI 的 Angular CLI 配置

ERROR:错误:

The ng set defaults.spec.component false command results in the error: get/set have been deprecated in favor of the config command. ng set defaults.spec.component false命令导致错误: get/set have been deprecated in favor of the config command.

ng set got changed to ng config . ng set更改为ng config

Using the Angular CLI (config command usage):使用 Angular CLI(配置命令用法):

The settings for generating specs, inline templates, inline styling etc. within angular.json are now persisted inside the schematics.@schematics/angular.<file-type>.<setting> .用于在angular.json中生成规范、内​​联模板、内联样式等的angular.json现在保存在angular.json schematics.@schematics/angular.<file-type>.<setting>

Run ng config schematics.@schematics/angular.component.spec false to configure spec for components.运行ng config schematics.@schematics/angular.component.spec false以配置组件的规范。 This command adds the setting inside the schematics property within the angular.json file.此命令在angular.json文件中的原理图属性中添加设置。


Angular CLI workspace file (angular.json) on Angular Github Angular Github 上的 Angular CLI 工作区文件 (angular.json)

Schematic options inside schema.json schema.json 中的示意图选项

How to do X in Angular CLI v6 如何在 Angular CLI v6 中执行 X

If you're using v6 and need to edit your angular.json如果您使用的是 v6 并且需要编辑 angular.json

You can edit the schematics for your 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
    }
  },

You can run this command to disable spec file generation for a specific type of file:您可以运行此命令来禁用特定类型文件的规范文件生成:

ng set defaults.spec.FILETYPE false

For example:例如:

ng set defaults.spec.component false // Won't generate spec files for .component files

Alternately you can just disable all spec file generation from the angular-cli.json file.或者,您可以从 angular-cli.json 文件中禁用所有规范文件生成。

{
  ...
  "defaults": {
    "spec": {
      "class": false,
      "component": false,
      "directive": false,
      "module": false,
      "pipe": false,
      "service": false
    }
  }
}

Just to update Sabbir Rahman's answer :只是为了更新Sabbir Rahman 的回答

In version 1.0.2 of the CLI you will have to set the spec file to false for each individual type.在 CLI 的 1.0.2 版中,您必须为每个单独的类型将规范文件设置为false An example is included below:下面包含一个示例:

"defaults": {
    "styleExt": "scss",
    "component": {
      "spec": false
    },
    "service": {
      "spec": false
    },
    "directive": {
      "spec": false
    },
    "class": {
      "spec": false // Set to false by default
    },
    "module": {
      "spec": false // Set to false by default
    },
    "pipe": {
      "spec": false
    }
  }

you can add --spec=false你可以添加--spec=false

example例子

ng g c home --spec=false

log will be日志将是

CREATE src/app/home/home.component.scss (0 bytes)
CREATE src/app/home/home.component.html (23 bytes)
CREATE src/app/home/home.component.ts (262 bytes)
UPDATE src/app/app.module.ts (467 bytes)

you can add --skipTests=true|false if true it wont generate any spec.ts您可以添加--skipTests=true|false如果为 true 则不会生成任何spec.ts

example : ng g component componentName --skipTests=true示例: ng g component componentName --skipTests=true

this line wont generate any spec.ts files此行不会生成任何spec.ts文件

EDIT:编辑:

Note: As of angular 7 this command doesn't work, even though this command is mentioned on the official website of angular.注意:从 angular 7 开始,此命令不起作用,即使在 angular 的官方网站上提到了此命令。 Here: https://angular.io/cli/generate#component在这里: https : //angular.io/cli/generate#component

For Angular 9+:对于 Angular 9+:

The Angular.json config has changed slightly which is causing previous configuration schemas to change, per this GitHub issue on Angular/CLI .根据Angular/CLI 上的这个GitHub 问题, Angular.json配置略有变化,这导致之前的配置模式发生变化。

Adapted from @sacgrover's comment along with my own:改编自@sacgrover 的评论以及我自己的评论

Old way老办法

"@schematics/angular:component": {
    // true => DO generate spec files, false => DON'T generate them
    "spec": true,
    "styleext": "scss"
}

New way:新方法:

"@schematics/angular:component": {
    // true => DON'T generate spec files, false => DO generate them
    "skipTests": true,
    "style": "scss"
}

Additional Reference => Angular Docs for CLI Generate Command附加参考 => CLI 生成命令的 Angular 文档

For angular 8+:对于角度 8+:

Option "spec" is deprecated: Use "skipTests" instead, so if you want to create a new component use:不推荐使用选项“spec”:改用“skipTests”,因此如果要创建新组件,请使用:

ng g c my-new-component --skipTests=true

您可以在创建应用程序时尝试--skip-tests ,如下所示。

ng new yourProjectName --skip-tests

Add Following code on Angular.json在 Angular.json 上添加以下代码

"schematics": {
    "@schematics/angular": {
      "component": {
        "spec": false
      }
    }
  }

只需运行此命令: ng config schematics.@schematics/angular.component.spec false

只要这样做,你应该没问题:

ng generate component newFile --skip-tests
"@schematics/angular:component": {"style": "scss","skipTests": true}

只需在angular.json文件中添加"skipTests":true即可解决问题

you can add --skipTests=true to stop generating spec file您可以添加--skipTests=true以停止生成规范文件

Example:例子:

ng g s core/services/auth/auth-guard --skipTests=true

log will be日志将是

CREATE src/app/core/services/auth/auth-guard.service.ts (138 bytes)

ng new my-app --skip-tests最初和任何时候你创建新组件都不会创建测试文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM