ng测试要求我在运行任何单元测试之前先修复所有棉绒

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ng测试要求我在运行任何单元测试之前先修复所有棉绒相关的知识,希望对你有一定的参考价值。

我的ng test的输出如下:

> ng test

24 12 2019 14:20:07.854:WARN [karma]: No captured browser, open http://localhost:9876/
24 12 2019 14:20:07.860:INFO [karma-server]: Karma v4.4.1 server started at http://0.0.0.0:9876/
24 12 2019 14:20:07.860:INFO [launcher]: Launching browsers ChromeNoSandbox with concurrency unlimited
24 12 2019 14:20:07.865:INFO [launcher]: Starting browser ChromeHeadless

app/shared/forms/templates/templates.component.ts:11:1 - error TS6133: 'MatDialog' is declared but its value is never read.

11 import { MatDialog } from '@angular/material';
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 12 2019 14:20:11.633:INFO [HeadlessChrome 79.0.3945 (Mac OS X 10.15.2)]: Connected on socket 9OfTSV7nahQakrDOAAAA with id 85821004
HeadlessChrome 79.0.3945 (Mac OS X 10.15.2): Executed 0 of 0 SUCCESS (0 secs / 0 secs)
HeadlessChrome 79.0.3945 (Mac OS X 10.15.2): Executed 0 of 0 SUCCESS (0.002 secs / 0 secs)
TOTAL: 0 SUCCESS
TOTAL: 0 SUCCESS

我遇到的问题是,如果我的src代码中出现any掉线错误,那么单元测试将无法运行,当我只想在这里和那里尝试测试驱动开发时,这非常不方便。我宁愿仅收到这些掉毛错误的警告,也可以立即修复所有问题。

我使用的是Angular版本,"@angular/core": "8.1.2"和Karma版本:

"karma": "4.4.1",
"karma-chrome-launcher": "3.1.0",
"karma-cli": "2.0.0",
"karma-coverage-istanbul-reporter": "2.1.1",
"karma-jasmine": "2.0.1",
"karma-jasmine-html-reporter": "1.4.2",
"karma-junit-reporter": "2.0.1",
"karma-verbose-reporter": "0.0.6",

在Angular 7上我没有任何问题,无论是否发生掉毛错误,我的单元测试都将执行。我不确定应该更改什么或应该检查哪种配置以阻止这种情况的发生。

最终目标是使单元测试能够运行,而不管毛发错误的发生频率如何。

angular.json包含测试的配置:

    "test": {
      "builder": "@angular-devkit/build-angular:karma",
      "options": {
        "main": "src/test.ts",
        "karmaConfig": "./karma.conf.js",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "src/tsconfig.spec.json",
    ...etc...

karma.conf

// Karma configuration file, see link for more information
// https://karma-runner.github.io/1.0/config/configuration-file.html

module.exports = function (config) {
  config.set({
    basePath: '',
    frameworks: ['jasmine', '@angular-devkit/build-angular'],
    plugins: [
      require('karma-jasmine'),
      require('karma-chrome-launcher'),
      require('karma-jasmine-html-reporter'),
      require('karma-junit-reporter'),
      require('karma-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma'),
      require('karma-verbose-reporter')
    ],
    client:{
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      dir: require('path').join(__dirname, 'coverage'), reports: [ 'html', 'lcovonly' ],
      fixWebpackSourcePaths: true
    },

    reporters: config.angularCli && config.angularCli.codeCoverage ? ['progress', 'coverage-istanbul', 'junit'] : ['progress', 'kjhtml'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['ChromeNoSandbox'],
    singleRun: false,
    junitReporter: {
      outputDir: process.env.JUNIT_REPORT_PATH,
      outputFile: process.env.JUNIT_REPORT_NAME,
      useBrowserName: false
    },
    customLaunchers: {
      ChromeNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    }
  });
};
答案

这是TypeScript错误,这意味着noUnusedLocals中的true被设置为tsconfig.spec.json

我认为您可以创建继承的配置,即tsconfig.spec.dev.json(使用“扩展”:“ ./ tsconfig.spec.json”),并用noUnusedLocals覆盖false值。然后,您需要添加configurations部分:

    "test": {
      "builder": "@angular-devkit/build-angular:karma",
      "options": {
        "main": "src/test.ts",
        "polyfills": "src/polyfills.ts",
        "tsConfig": "tsconfig.spec.dev.json",
        "karmaConfig": "karma.conf.js",
        "assets": [
          "src/favicon.ico",
          "src/assets"
        ],
        "styles": [
          "src/styles.css"
        ],
        "scripts": []
      },
      "configurations": {
        "production": {
          "tsConfig": "tsconfig.spec.json"
        }
      }
    }

现在您可以将dev作为ng test或prod作为ng test --prod

以上是关于ng测试要求我在运行任何单元测试之前先修复所有棉绒的主要内容,如果未能解决你的问题,请参考以下文章

Angularjs单元测试-将文本添加到textarea时禁用ng的不起作用

运行所有测试时单元测试失败,但调试时通过

[在运行单元测试(UWP)时启动应用程序

Angular Schematics:在库中运行单元测试时出错

XCode UITest:测试运行时模拟器不可见

JavaEE之Junit单元测试