简体   繁体   English

Angular CLI /使用ng test运行两类测试

[英]Angular CLI / Run two categories of test with ng test

I have two different types of tests: 我有两种不同类型的测试:

  • Unit tests (standard angular cli .spec.ts files) 单元测试(标准cli.spec.ts角文件)
  • Functional tests (my specific .func.spec.ts files, works the same way as .spec.ts files, but not mocked and injects data in my db, so no need to run them all the time) 功能测试(我特定的.func.spec.ts文件,与.spec.ts文件的工作方式相同,但未进行模拟并在我的数据库中插入数据,因此无需一直运行它们)

I am trying to : 我在尝试着 :

  • Exclude the .func.spec.ts files from the ng test command to only run .spec.ts test file ng test命令中排除.func.spec.ts文件,仅运行.spec.ts测试文件
  • Call the test command to only run .func.spec.ts 调用测试命令仅运行.func.spec.ts

So my functional tests are not executed in the regular ng test context and can be run separately. 因此,我的功能测试不会在常规ng test上下文中执行,而是可以单独运行。

In other words the solution can be... 换句话说,解决方案可以是...

  • npm run test runs .spec.ts files npm run test运行.spec.ts文件
  • npm run test:func runs .func.spec.ts files npm run test:func运行.func.spec.ts文件

I tried to create a different karma config file that excludes the files I don't want but it does not work. 我试图创建一个不同的业力配置文件,该文件排除了我不想要的文件,但是它不起作用。

Do you have any idea of how to perform this? 您对如何执行此操作有任何想法吗?

What you can do is to add a specific Karama configuration karma.func.conf.js 您可以做的是添加特定的Karama配置karma.func.conf.js

module.exports = function (config) {
  config.set({
    basePath: '',
    files: ['src/app/**/**.func.ts'] // Not 100% sure about the pattern
    frameworks: ['jasmine', '@angular/cli'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular/cli/plugins/karma')
    ],
    client:{
      clearContext: false
    },
    coverageIstanbulReporter: {
      reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },
    angularCli: {
      environment: 'dev' //you could change to use prod
    },
    reporters: ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false
  });
};

Add to your package.json 添加到您的package.json

"scripts": {
  "ng": "ng",
  "start": "ng serve",
  "build": "ng build --prod",
  "test": "ng test",
  "test:func": "ng test --config karma.func.conf.js"
  "lint": "ng lint",
  "e2e": "ng e2e"
}

Maybe there is another solution that will avoid maintaining two configuration files. 也许还有另一种解决方案可以避免维护两个配置文件。

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

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