ng test要求我在运行任何单元测试之前解决所有问题

时间:2019-12-24 14:38:21

标签: angular typescript unit-testing karma-jasmine tslint

我的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代码中出现任何条错误,则单元测试将无法运行,这在我只想在这里和那里尝试时非常不方便。测试驱动的开发。我宁愿只收到这些掉毛错误的警告,也可以立即解决所有问题。

我使用的是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']
      }
    }
  });
};

1 个答案:

答案 0 :(得分:3)

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

我认为您可以创建继承的配置,即false(使用“ extends”:“ ./tsconfig.spec.json”),并用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" } } } 的值。然后,您需要添加ng test部分:

ng test --prod

现在您可以以from typing import List def copy_case(a: str, b: str) -> str: res_chars: List[str] = [] curr_a: str curr_b: str for curr_a, curr_b in zip(a, b): if curr_a.isupper(): curr_b = curr_b.upper() else: curr_b = curr_b.lower() res_chars.append(curr_b) return ''.join(res_chars) print(copy_case('tEXt', 'xTTe')) 的身份运行dev或以projectPage: - Slideshow Banner: Link1: 'https://hi.com' Link2: 'https://bye.com' - Images: Image1: 'https://hi.com' Image2: 'https://bye.com' 的身份运行prod。