【问题标题】:Angular CLI / Run two categories of test with ng testAngular CLI / 使用 ng test 运行两类测试
【发布时间】:2018-09-30 23:03:21
【问题描述】:

我有两种不同类型的测试:

  • 单元测试(标准 Angular cli .spec.ts 文件)
  • 功能测试(我的特定 .func.spec.ts 文件,与 .spec.ts 文件的工作方式相同,但未模拟并将数据注入我的数据库,因此无需运行它们一直)

我正在尝试:

  • ng test 命令中排除 .func.spec.ts 文件以仅运行 .spec.ts 测试文件
  • 调用测试命令只运行.func.spec.ts

所以我的功能测试不在常规的ng test 上下文中执行,可以单独运行。

换句话说,解决方案可以是……

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

我尝试创建一个不同的 karma 配置文件,排除我不想要的文件,但它不起作用。

您知道如何执行此操作吗?

【问题讨论】:

  • 可以在package.json文件中编辑
  • @Azalkor 问题不是命令,问题是两种测试执行的文件排除
  • 您应该能够编辑运行 karma *.func.spec.ts 的“test:func”命令,不是吗?

标签: angular angular-cli


【解决方案1】:

你可以做的是添加一个特定的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
  });
};

添加到你的 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"
}

也许还有另一种解决方案可以避免维护两个配置文件。

【讨论】:

  • 这不起作用,因为 Karma 的 Angular CLI 特殊性。添加 files: [ '**/*.func.spec.ts', 时,每个测试仍在运行
  • 你说得对,问题是当前的 angular cli karma 插件会在 test.ts 中添加 spec.ts。所以你需要改变它,但我不确定你如何动态改变它。
猜你喜欢
  • 1970-01-01
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多