Get Component without spec.ts file in Angularjs 2

2020-05-20 05:21发布

Is there a way to get rid of the spec.ts file in Angularjs 2, whenever I create a new component. I know this is for testing purpose but what if I don't need it.

May be there is some setting to disable this specific testing file.

18条回答
冷血范
2楼-- · 2020-05-20 05:45
ng g c component-name --spec false
查看更多
家丑人穷心不美
3楼-- · 2020-05-20 05:45

Just adding this bit of information because I found myself struggling with the same issue and could not fix it by using the current answer(s), plus I did not want to make the changes on the angular-cli.json file. It appears the recent version of Angular CLI --spec=false is depreciated and therefore no longer works, use --skipTests instead.

Test on:

Angular CLI: 9.1.1
Node: 12.16.1
OS: win32 x64

Your command with then be: ng g c mycomponent --skipTests instead of ng g c mycomponent --spec=false

PS: Always test your commands using the --dry-run option to test your output.

查看更多
Fickle 薄情
4楼-- · 2020-05-20 05:46

For angular 6+ Simply run this command:

ng config schematics.@schematics/angular.component.spec false

OR

just add this in your angular.json file and you're good to go

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

NOTE: before pasting this check for conflicts, hope it helps

查看更多
相关推荐>>
5楼-- · 2020-05-20 05:52

When using angular-cli there is a bunch of options to pass. To generate a new component without tests, you can simply add --spec=false like so:

ng generate component --spec=false my-component

There is also an option in the angular-cli.json for which types you want to enable specs by default, where you can modify the behaviour:

"defaults": {
  "spec": {
    "class": false,
    "component": true,
    "directive": true,
    "module": false,
    "pipe": true,
    "service": true
  }
}
查看更多
再贱就再见
6楼-- · 2020-05-20 05:52

You can use the following command when you create your component

ng g c component-name --spec false
查看更多
戒情不戒烟
7楼-- · 2020-05-20 05:54

In recent Angular versions you can configure the cli generator to disable the creation of spec file for components and services in angular-cli.json as follows:

"defaults": {
    "component": { "spec": false },
    "service": { "spec": false },
    ...
}

You can add extra lines to disable specs also for guards and classes and so on.

查看更多
登录 后发表回答