Table of contents
1.
Introduction
2.
test.skip
2.1.
Program
2.2.
Output
3.
test.skip.each
3.1.
Program
3.2.
Output
3.3.
Program
3.4.
Output
4.
describe.skip
4.1.
Program
4.2.
Output
5.
test.only & describe.only
5.1.
Program
5.2.
Output
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Skipping a Test with Jest

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will discuss a very crucial concept related to skipping test cases with jest. It often occurs that we do not need to run all the test cases or we need to execute only specific tests against our codebase. While we can simply comment-out the test cases which are not intended to be executed, it's often a bit nicer to use test.skip because it will maintain indentation and syntax highlighting. This blog will describe examples to skip tests in jest.

test.skip

Let’s say we have three tests, namely, test 1, test 2, and test 3. But we need to skip test 2 due to certain reasons. In jest, we can do this using the test.skip function. Using this function, we can skip specific test cases. Let us see an example code to understand it better.

We will use a describe block to group the tests together.

Create a program named skip.test.js with the following program and run the test using npm run test.

Program

describe('A: Demonstrate Skip test', () => {
   test('test 1', () => {
       console.log('test 1 complete')
   })

   test.skip('test 2', () => {
       console.log('test 2 complete')
   })

   test('A: test 3', () => {
       console.log('test 3 complete')
   })
})
You can also try this code with Online Javascript Compiler
Run Code

Output

test.skip.each

What if you want to run the same test on a set of data? As you know we can use test.each function in such a scenario. test.each function accepts similar arguments as test function. Except, it also accepts the set of data on which the test is to be run.

Let us see an example to better understand. Create a program named skip.test.js with the following program and run the test using npm run test.

Program

test.each([
   [7, 5, 12],
   [12, 12, 24],
   [2, 4, 6],
])('.add(%d, %d)', (v1, v2, resExpected) => {
   expect(v1 + v2).toBe(resExpected); // will not be ran
});
You can also try this code with Online Javascript Compiler
Run Code

Output

 

Now, if you want to skip the tests in this function, you just need to use skip.test.each function as shown in the program below.

Program

test.skip.each([
   [7, 5, 12],
   [12, 12, 24],
   [2, 4, 6],
])('.add(%d, %d)', (v1, v2, resExpected) => {
   expect(v1 + v2).toBe(resExpected); // will not be ran
});

test('Test executed', () => {
   expect(1 / 0).toBe(Infinity);
});
You can also try this code with Online Javascript Compiler
Run Code

Output

describe.skip

We can also skip whole describe blocks using describe.skip function. The arguments of this function are the same as that of describe function in jest.

Let us see an example.

Program

describe.skip('A: Skip this block', () => {
   test('A: test 1', () => {
       console.log('A: test 1 complete')
   })

   test('A: test 2', () => {
       console.log('A: test 2 complete')
   })

})

describe('B: Only run this suite', () => {
   test('B: test 1', () => {
       console.log('B: test 1 complete')
   })

   test('B: test 2', () => {
       console.log('B: test 2 complete')
   })
})
You can also try this code with Online Javascript Compiler
Run Code

Output

test.only & describe.only

Just opposite to the test.skip and describe.skip function, we can use test.only and describe.only functions if we want to run specific describe and test blocks. These functions are best for debugging purposes.

Let us see an example.

Program

describe('A: This describe block won\'t be executed', () => {
   test('A: test 1', () => {
       console.log('A: test 1 complete')
   })

   test('A: test 2', () => {
       console.log('A: test 2 complete')
   })
})

describe.only('B: Only run this describe block', () => {
   test('B: test 1', () => {
       console.log('B: test 1 complete')
   })

   test('B: test 2', () => {
       console.log('B: test 2 complete')
   })
})
You can also try this code with Online Javascript Compiler
Run Code

Output

FAQs

  1. How to ignore test cases in jest?
    We can use test.skip, describe.skip functions to skip individual and blocks of tests in jest.
     
  2. How to run all jest tests?
    If you want to re-run all tests when a file has changed, use the --watchAll option instead.
     
  3. How do you run test cases sequentially in Jest?
    Rather than generating a worker pool of child processes to conduct tests, run all tests sequentially in the current process (...) Jest is typically executed by a single parent dispatcher process, which deploys child-processes as workers to run your tests in parallel.
     
  4. How do you debug a Jest test?
    To debug the code that you have written, you can use describe.only and test.only functions to run only specific tests and describe blocks. Using this, you can run only those tests that are meant to test particular sections of code.

Key Takeaways

In this blog, we discussed how to skip test cases in Jest. We learned how to write the test.skip and describe.skip functions to skip test cases. We also learned how to use the test.each.skip function to skip the same test over multiple data. We also saw the use of test.only and describe.only functions to run specific test cases and describe blocks.

You may want to learn about mock functions in Jest after this. Find out an interesting article here.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass