user11767946
user11767946

Reputation:

How to run a specific test file in Karma? (Angular 4+)

When I run ng test, it goes through all test files.

I found an answer, by running the providing the path of the file:

ng test --main src/app/pages/home-modal/home-modal.component.spec.ts

on stackoverflow but I guess it was deprecated and I received this error:

AsyncTestZoneSpec is needed for the async()

Here is my karma.config.ts file:

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-coverage-istanbul-reporter'),
      require('@angular-devkit/build-angular/plugins/karma')
    ],
    client: {
      clearContext: false // leave Jasmine Spec Runner output visible in browser
    },
    coverageIstanbulReporter: {
      reports: ['html', 'lcovonly', 'text-summary'],
      fixWebpackSourcePaths: true
    },
    reporters: ['progress', 'kjhtml'],
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Chrome'],
    singleRun: false,
    restartOnFileChange: true
  });

};

Does anyone know the new way of running a specific file in Karma for Angular 4+?

Upvotes: 4

Views: 8196

Answers (2)

Johns Mathew
Johns Mathew

Reputation: 812

To run a single test file, change the describe block in the file to fdescribe and run the tests as usual using ng test.

You can also use fit to run a single test case alone.

Upvotes: 3

Wesley Trantham
Wesley Trantham

Reputation: 1264

In your test.ts file you should have a line for

const context = require.context('./', true, /\.spec\.ts$/);

That last part of /\.spec\.ts$/ is regex for load any find that ends with .spec.ts.

You can change this (temporarily) to be /your\.component\.spec\.ts$/ and then run npm test as normal

Upvotes: 1

Related Questions