ng test requires me to fix all linting before running any unit tests
Asked Answered
E

1

6

The output of my ng test is as follows:

> 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

The issue I am having is that if I have any linting errors in my src code, then unit tests will not run, which is extremely inconvenient when I just want to try things here and there for test-driven development. I would much rather just get the warnings for these linting errors and I can fix them all at once.

I am on Angular version, "@angular/core": "8.1.2" and Karma versions:

"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",

On Angular 7 I did not have a problem with this, my unit tests would execute regardless of linting errors. I am not sure what has changed or which configuration I should be checking to stop this from occurring.

The end goal is for unit tests to be able to run regardless of any frequency of linting errors.

angular.json contains the configuration for tests:

    "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']
      }
    }
  });
};
Endear answered 24/12, 2019 at 14:38 Comment(2)
Does it behave the same if you run it with karma start?Earthiness
I get 28 01 2020 08:56:56.615:ERROR [karma-server]: Server start failed on port 9876: Error: The '@angular-devkit/build-angular/plugins/karma' karma plugin is meant to be used from within Angular CLI and will not work correctly outside of it. when trying to run with karma startEndear
P
3

This is TypeScript error, it means that noUnusedLocals is set to true in tsconfig.spec.json.

I think you can create inherited config i.e. tsconfig.spec.dev.json (use "extends": "./tsconfig.spec.json") and override noUnusedLocals value with false. Then you need to add configurations section:

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

Now you can run dev as ng test or prod as ng test --prod.

Polaris answered 18/2, 2020 at 22:3 Comment(2)
Sorry, this is occurring on any linting errors. I want to ignore any linting errors and allow tests to still run but then still notify me of the linting errors. So effectively I would want noUnusedLocals to be set to something like warn rather than false. Setting to false would allow me to run tests but then I wouldn't see that errorEndear
It's compiler error and you will have to turn off all of them. Your IDE will most probably pick tsconfig.json for its language server so you will still see all errors in editor.Polaris

© 2022 - 2024 — McMap. All rights reserved.