99

每当我创建一个新组件时,有没有办法摆脱 Angular 2+ 中的 spec.ts 文件。我知道这是出于测试目的,但如果我不需要它怎么办。

可能有一些设置可以禁用这个特定的测试文件。

4

29 回答 29

222

针对 Angular >=8 CLI 更新

对于一个组件,使用以下命令:

ng generate component --skip-tests=true component-name

对于单个项目,更改或添加以下内容angular.json

{ 
  "projects": {
    "{PROJECT_NAME}": {
      "schematics": {
        "@schematics/angular:component": {
          "skipTests": true
        }
      }
    }
  }
}

对于所有项目的全局设置,请在您的 中更改或添加以下内容angular.json

{ 
  "schematics": {
    "@schematics/angular:component": {
      "skipTests": true
    }
  }
}

或者使用命令行

ng config schematics.@schematics/angular:component.skipTests true

< 角 8

在您angular-cli.json将 spec.component 参数设置为false

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

--spec=false在创建期间使用该选项

ng generate component --spec=false component-name
于 2016-12-06T07:57:22.017 回答
19

对于角 6

ng config schematics.@schematics/angular.component.spec false
于 2018-06-23T12:09:57.413 回答
11

对于Angular 6+,只需运行以下命令:

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

或者

只需将其添加到您的 angular.json 文件中即可

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

注意:在粘贴此冲突检查之前,希望它有所帮助

于 2018-10-02T19:33:42.817 回答
10

使用 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
  }
}
于 2016-12-06T07:57:51.813 回答
9

对于 Angular 7 及更高版本,这将起作用:

ng generate component componentname --skipTests

或者 ng generate component componentname --skipTests=true

于 2019-06-19T20:14:32.067 回答
7

最新的 Angular 9 兼容

命令行命令

ng generate component your-component-name --skipTests=true

使用别名s代替--skipTests

ng generate component your-component-name -s=true

修改angular.json以避免始终使用上述 CLI 命令

将以下代码段复制到projects.your-project-nameangular.json.

下面将确保不会使用命令.specComponentsClassDirectivesPipeServiceng generate创建文件。您可以根据需要选择那些。

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

PS:该--spec=false选项现已弃用,将不起作用

于 2020-06-14T10:33:34.807 回答
5

在最近的 Angular 版本中,您可以配置 cli 生成器以禁用为组件和服务创建规范文件,angular-cli.json如下所示:

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

您可以添加额外的行来禁用守卫和类等的规范。

于 2017-12-10T19:12:02.887 回答
4

只需在 Angular 7 中使用这个简单的命令创建组件

ng g c component-name --spec false

或者如果真的不想将它包含在任何组件中,只需更新 angular.json

"@schematics/angular": {
  "component": {
    "spec": false
  }
}
于 2018-11-27T13:23:10.413 回答
4

简单地试试这个。这是有效的

ng g c component_name --spec false
于 2018-05-02T14:10:54.917 回答
4

默认情况下,有一个用于禁用 Angular 生成“规范”文件的命令

ng set defaults.class.spec=false
ng set defaults.component.spec=false

更新:对于 Angular 6

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

于 2018-01-16T15:39:42.853 回答
3

对于 Angular 8:ng generate component mycomponent --skipTests=false

于 2019-03-26T15:00:20.533 回答
2

--spec将不再工作。改为使用 --skip-tests

您将使用以下命令看到所有选项:

ng g c --help
于 2020-05-23T01:38:53.147 回答
2

这里是 angular 9

ng g class models\user --skip-tests true

它不会生成 spec.ts 文件。

于 2020-05-07T15:08:18.583 回答
2
ng g c component-name --spec false
于 2017-06-14T03:10:59.630 回答
1

根据修复 #156,您可以使用创建没有规范文件的组件

ng generate component my-comp --nospec
于 2016-12-06T07:58:14.817 回答
1

回复:在Angular 2+中生成没有spec.ts文件的组件

ng gc ComponentName --skip-tests true

于 2021-05-23T09:33:30.253 回答
1

对于 Angular 6,在 angular.json 里面添加这个"schematics":{}

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

或者

正如富兰克林虔诚所建议的那样,运行以下命令:

ng config schematics.@schematics/angular.component.spec false
于 2018-09-30T18:52:07.927 回答
1

对于角度 8+,您只需要添加

--skipTests=true

到底

ng g c component-name --skipTests=true

g 缩写为生成。c 缩写为组件

你也可以使用

ng generate component component-name --skipTests=true
于 2020-11-24T09:28:34.613 回答
1

这很容易做到。在创建组件时添加

--skipTests=true

ng generate component --skipTests=true componentName

或者

ng g c --skipTests=true componentName
于 2020-09-09T06:31:44.347 回答
1

如果您想跳过特定组件的规范文件。

新版本:

ng generate component "componentName" --skipTests

例子

ng generate component recipe-list --skipTests

旧版 :

ng generate component "componentName" --spec false

例子

ng generate component recipe-list --spec false
于 2020-05-25T14:49:54.490 回答
0

如果您想跳过您的 spec.ts 文件,还可以使用以下命令:

ng g c try-it --skipTests=true

在哪里

g c=> 生成组件, try-it=> 组件名称, --skipTest=true=> == -- spec false

于 2019-11-11T09:08:35.403 回答
0

它对我有用angular 11

ng g c posts/new-component --module app --skip-tests

笔记:

组件的名称应该是这样的:new-componentnewComponent

为特定文件夹生成:dir/new-component

添加new-componentapp.module.ts--module app

--skip-tests在命令中使用生成没有spec.ts文件的组件

于 2021-04-10T09:21:00.567 回答
0

使用时:

  • 角 CLI:10.0.3
  • 节点:12.16.3
  • 角度:10.0.4

生成组件时必须使用“--skip-tests true”。(注意:您可以通过在终端中尝试“ng v”或“ng -v”来获取上述版本信息)。

您可以使用“ng gc --help”获取“ng gc(ng generate component 的缩写)”的完整开关列表。

或者,您可以手动添加组件。为此,请通过右键单击项目的 /src/app 文件夹来创建一个新文件夹。创建文件夹后,右键添加一个或多个文件,如mycomp.component.html和mycomp.component.ts。添加 mycomp.component.ts 是组件的最低要求列表,因为您可以将内联 css 和 html 添加到 .ts 文件中。

例子:

import { Component } from '@angular/core';

@Component ({ 选择器: 'app-mycomp', 模板: ' <p> Hello Angular </p>' })

导出类 MyComp {}

<app-mycomp></app-mycomp在 app.module.ts 声明列表中导入并注册 MyComp,并使用选择器( > )将其添加到 app.component.html 或其他模板中

于 2020-07-22T00:50:55.083 回答
0

对于 Angular 10+ - 用于所有项目的全局设置。

  1. 转到 angular.json 文件

  2. 在“原理图”字段中添加:

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

** 也可以用于服务、警卫等...

    "@schematics/angular:service": {
      "skipTests": true
    },
    "@schematics/angular:guard": {
      "skipTests": true
    }
于 2022-01-12T10:57:08.547 回答
0

在您的项目中进入angular.json文件并在原理图字段中添加spec - false

保存并重新启动您的项目现在您可以创建所有没有规范文件的组件

 "schematics": {
                "@schematics/angular:component": {
                    "style": "scss",
                    "spec": false
                }
            },
于 2022-01-05T11:46:19.080 回答
0

创建组件时可以使用以下命令

ng g c component-name --spec false
于 2019-09-02T18:27:33.207 回答
0

只是添加了这一点信息,因为我发现自己在同一个问题上苦苦挣扎,并且无法通过使用当前答案来解决它,而且我不想对angular-cli.json文件进行更改。Angular CLI 的最新版本似乎已--spec=false被贬低,因此不再有效,请--skipTests改用。

测试:

Angular CLI: 9.1.1
Node: 12.16.1
OS: win32 x64

你的命令 then 是: ng g c mycomponent --skipTests 而不是 ng g c mycomponent --spec=false

PS:始终使用--dry-run测试输出的选项来测试您的命令。

于 2020-05-01T13:10:03.973 回答
0

角度 < 8 $ ng g component component-name --spec=false

角度 > 8 $ ng g component component-name --skipTests=false --flat

   $ ng g component employee/listEmployees --skipTests=false --flat
于 2020-08-18T16:00:52.903 回答
-2

对于 Angular 9,您可以使用

ng g c "componentName" --spec=false

生成没有规范文件的组件

于 2020-04-25T18:45:36.110 回答