4 ways to skip a test case execution in angular


In this tutorial, You learn different ways to skip spec.ts file or test case execution in angular application. This also includes exclude single or group of spec file from jasmine and mocha with karma runner

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

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

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

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

Running 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 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 regular expression to exclude single or group of test case execution This will run all test cases except my.component.spec.ts file while running ng test command.

jasmine and karma disable test case execution

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

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

Jasmine and karma provides 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 allows skip method to skip single and multiple tests

  • describe.skip - Allows to skip 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 compile and running for code coverage, you want to exclude some specific files or folders, you can configure in angular.json

angular.json

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

   }

Conclusion

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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.