4 ways to skip a test case execution in angular

In this tutorial, You learn different ways to skip spec.ts files or test case execution in testing angular applications. It includes excluding a single or group of spec files from jasmine and mocha with the karma test runner.

In my previous post, You learned different ways to [run a one spec.ts test case](/angular-run-single-testfile) an Angular app.

By default, an Angular application has a test.js file located in the src folder.

This file contains the below line of code which configures to include test files while running the ng test or ng t command.

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

Running the ng test command executes all test spec files of an angular project.

ng test
(or)
ng t

if you have a my.component.spec.ts that you want to exclude a component test file

There are multiple ways we can do

configure test.js to exclude test spec component execution

Let’s configure the my.component.spec.ts spec file to skip test case execution in test.js

const context = require.context("./", true, /^((?!my.component).)*\.spec.ts$/);

It uses a regular expression to exclude a single or group of test case execution. It runs all test cases except the my.component.spec.ts file while running the ng test command.

jasmine and karma disable test case execution

normally we have a description for group of tests and it has a single test method for writing unit testing in angular

describe('MyComponent', () => {
  it('should create', () => {
    //test for undefined
  }
}

Jasmine and karma provide xdescribe and xit allows you to disable specific test cases execution.

xdescribe('MyComponent', () => {
  xit('should create', () => {
    //test for undefined
  }
}

Please note that use ddescribe for jasmine version <2.1 version, and use fdescribe for >2.1 version.

How to skip test component spec file with mocha and karma

mocha and karma allow the skip method to skip single and multiple tests

  • describe.skip- Allows to skip a group of tests
  • it.skip - Allows to skip a single test case
describe.skip('MyComponent', () => {
  it.skip('should create', () => {
    //test for undefined
  }
}

How to exclude test case execution for code coverage report

when you are compiling and running for code coverage, you want to exclude some specific files or folders, which you can configure in angular.json

angular.json

   "test": {
        "options": {
              "codeCoverageExclude": [
                    "/**/*service*.ts"
                ]
        }

   }

Conclusion

You learned multiple ways to skip a single or group of tests of an angular application.