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

时间:2017-02-05 07:56:57

标签: angular typescript angular-cli angular2-cli

我知道这是一种糟糕的做法,但与我配对:

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

因此,对于每次运行,是的,我可以这样做--spec

但是如何全局禁用测试文件的创建?有什么默认设置吗?

Rashly,我做了一些像(没有用)的东西:

ng g c foo --it --is --spec=false

然后尝试通过填充排除数组来配置ts设置文件ng set spec=false --global

src/tsconfig.json

12 个答案:

答案 0 :(得分:36)

对于Angular 6>

配置(例如生成规范)可以手动完成,也可以使用Angular CLI完成。

手动Angular CLI配置

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

每种文件类型的所有可配置选项(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 have been deprecated in favor of the config command.

ng set got changed to ng config

使用Angular CLI(配置命令用法):

angular.json内生成规格,内联模板,内联样式等的设置现已保留在schematics.@schematics/angular.<file-type>.<setting>内。

运行ng config schematics.@schematics/angular.component.spec false以配置组件规范。此命令在angular.json文件中的schematics属性中添加设置。

Angular CLI workspace file (angular.json) on Angular Github

Schematic options inside schema.json

How to do X in Angular CLI v6

答案 1 :(得分:24)

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

ng set defaults.spec.FILETYPE false

例如:

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

或者,您可以从angular-cli.json文件中禁用所有规范文件生成。

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

答案 2 :(得分:24)

如果您使用的是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
    }
  },

答案 3 :(得分:12)

更新Sabbir Rahman's answer

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

答案 4 :(得分:9)

对于Angular 9 +:

GitHub issue on Angular/CLIAngular.json配置略有变化,这导致以前的配置方案发生了变化。

@sacgrover's comment一起从my own:改编

旧方式

"@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 Docs for CLI Generate Command

答案 5 :(得分:8)

对于角度8以上的用户:

不建议使用“ spec”选项:请改用“ skipTests”,因此,如果要创建新组件,请使用:

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

答案 6 :(得分:3)

您可以添加--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)

答案 7 :(得分:1)

在Angular.json上添加以下代码

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

答案 8 :(得分:0)

您可以添加EnableSAMLSSOLogin=true EnableOpenIDLogin=true EnableSAML2Grant=false #This is the URL of the page that is used to choose the login scheme #such as SAML SSO or OpenID. This Url will not be processed by the #SSOAgentFilter LoginUrl=index.jsp #Url to do send SAMLSSO AuthnRequest SAMLSSOUrl=samlsso #Url to do send SAML2 Grant OAuth2 Request SAML2GrantUrl=token #Url to send OpenID Authentication Request OpenIDUrl=openid #A unique identifier for this SAML 2.0 Service Provider application SAML.IssuerID=travelocity.com #The URL of the SAML 2.0 Assertion Consumer SAML.ConsumerUrl=http://localhost:8080/travelocity.com/home.jsp #The URL of the SAML 2.0 Identity Provider SAML.IdPUrl=https://localhost:9443/samlsso #This is the attribute name under which the authenticated session information #of SAML SSO and OpenID are stored SSOAgentSessionBeanName=SSOAgentSessionBean #Identifier given for the Service Provider for SAML 2.0 attributes #exchange #SAML.AttributeConsumingServiceIndex=1701087467 #Specify if SingleLogout is enabled/disabled SAML.EnableSLO=true #This is the URL that is used for SLO SAML.LogoutUrl=logout #Specify if SAMLResponse element is signed SAML.EnableResponseSigning=true #Specify if SAMLAssertion element is signed SAML.EnableAssertionSigning=true #Specify if AuthnRequests and LogoutRequests should be signed SAML.EnableRequestSigning=true #Custom credentials class SAML.SSOAgentCredentialImplClass=org.wso2.carbon.identity.sso.agent.saml.SSOAgentKeyStoreCredential #KeyStore to cryptographic credentials #KeyStore=/home/johann/Desktop/wso2is-4.1.0/repository/resources/security/wso2carbon.jks #Password of the KeyStore for SAML and OpenID KeyStorePassword=wso2carbon #Alias of the IdP's public certificate SAML.IdPCertAlias=wso2carbon #Alias of the SP's private key SAML.PrivateKeyAlias=wso2carbon #Private key password to retrieve the private key used to sign #AuthnRequest and LogoutRequest messages SAML.PrivateKeyPassword=wso2carbon #OAuth2 token endpoint URL SAML.OAuth2TokenEndpoint=https://localhost:9443/oauth2/token #OAuth2 Client ID SAML.OAuth2ClientID=Qn5DQHCYfshxeZh6R9SL1HM2lsMa #OAuth2 Client Secret SAML.OAuth2ClientSecret=cbkAs1gajdwPAMbrSR54hPAIcz0a #OpenId Provider Url OpenID.OpenIdProviderUrl=https://localhost:9443/openid/ #openid.return_to parameter OpenID.ReturnToUrl=http://localhost:8080/travelocity.com/home.jsp #This is the request parameter name under which to find the #openid.claimed_id value to send OpenID authentication request OpenID.ClaimedIDParameterName=claimed_id #Custom OpenID AttributesRequestor class OpenID.AttributesRequestorImplClass=SampleAttributesRequestor 如果为true,则不会生成任何 spec.ts

示例:--skipTests=true|false

此行不会生成任何 spec.ts 文件

答案 9 :(得分:0)

按如下所示创建应用时可以尝试--skip-tests

ng new yourProjectName --skip-tests

答案 10 :(得分:0)

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

答案 11 :(得分:0)

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

ng generate component newFile --skip-tests