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

87

我知道这并不是好的实践方式,但请和我一起忍耐:

我正在使用 Angular-CLI,特别是 ng g 来生成我的所有类。然而,我对任何 *.spec.ts 测试文件都不感兴趣。我知道有两个标志(--inline-template--inline-style)用于处理内联 CSS 和 HTML,而对于规范,--spec 标志默认设置为 true。

因此,对于每次运行,是的,我可以执行 ng g c foo --it --is --spec=false

但是如何全局禁用测试文件的创建呢?是否有任何默认设置可以实现这一点?

我冲动地尝试了一些(没有奏效的)操作:

ng set spec=false --global

我也尝试通过填充src/tsconfig.json中的排除数组来进行配置:

"exclude": [
    "**/*.spec.ts"
]
14个回答

156

对于Angular 9+(版本6+以下)

1)将片段复制到angular.json的根目录,(配置所有项目/全局设置)。
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
  }
},

每种文件类型的所有可配置选项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
  }
},

适用于 Angular 6+

1)将代码片段复制到 angular.json 根目录中(配置所有项目 / 全局设置)。
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
  }
},

每种类型文件的所有可配置选项 (原理图选项):

"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 已经过时,建议使用 config 命令。

ng set 已更改为 ng config

使用 Angular CLI(使用 config 命令):

生成规格、内联模板、内联样式等设置现在保存在 schematics.@schematics/angular.<file-type>.<setting> 中的 angular.json 文件中。

运行 ng config schematics.@schematics/angular.component.spec false 来配置组件的规格。该命令会将设置添加到 angular.json 文件的 schematics 属性中。


Angular Github 上的 Angular CLI 工作空间文件(angular.json)

schema.json 中的原理图选项

如何在 Angular


29

如果您正在使用v6并需要编辑您的angular.json文件

您可以编辑项目的模式。

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

1
请在项目的angular.json文件中添加以下代码以解决Angular 6问题。您应该使用带有“Angular Files”插件的VScode工具来创建服务、组件、模块和路由等。 - HungNM2
1
谢谢,它也适用于Angular 7。添加到angular.json文件的末尾。 - Surendranath Sonawane

24

您可以运行此命令来禁用特定类型文件的规范文件生成:

ng set defaults.spec.FILETYPE false

例如:

ng set defaults.spec.component false // 不会为 .component 文件生成规范文件

或者,您可以通过编辑 angular-cli.json 文件来完全禁用规范文件的生成。

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

5
angular-cli.json已被弃用,推荐使用angular.json。并且angular.json中没有defaults键,因此我担心这个答案已经过时。 - Patrick

15

针对 Angular 9+:

Angular.json 配置略有变动,导致之前的配置模式需要更改,详见 Angular/CLI 的 GitHub issue

以下内容结合了 @sacgrover 的评论以及我的意见:

旧方式:

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

新方式:

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

附加参考 => Angular CLI生成命令的文档


属性“@schematics/angular:component”不被允许(Angular 10)。 - Goran_Ilic_Ilke

13

仅作更新Sabbir Rahman的答案

在CLI的1.0.2版本中,您需要为每个独立类型设置spec文件为false。以下是一个示例:

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

12

如果设置为true,您可以添加--skipTests=true|false, 它将不会生成任何spec.ts文件。

例如:ng g component 组件名称 --skipTests=true

这条命令将不会生成任何spec.ts文件。

注:

注意:自Angular 7以来,即使此命令在官方网站上被提及,它也不能正常工作。

链接:https://angular.io/cli/generate#component


1
不想给你的回答打负分,但它不起作用(我在ng7中尝试过了)... - TheCuBeMan
3
尝试使用Angular 9,这种方法有效。我更喜欢这种方式,而不是通过JSON禁用所有规范文件。 - Andrew

11

您可以添加--spec=false

例子

ng g c home --spec=false

日志将会被记录

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)

9

针对 Angular 8+:

"spec"选项已弃用,请使用"skipTests"代替,因此如果您想创建一个新组件,请使用以下命令:

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

这已经过时了!! - Lakshman Pilaka

7
"@schematics/angular:component": {"style": "scss","skipTests": true}

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


3

在创建应用程序时,您可以像下面这样尝试使用 --skip-tests

ng new 你的项目名称 --skip-tests


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接