Jest How to skip single or suite of tests with examples

This tutorial explains how to skip unit tests in Jest Framework.

  • How to skip a single test in jest
  • How to skip test suite in a jest
  • Jest it only tests example
  • Jest cli to ignore test files

How to skip single test cases in Jest

First, Let’s write a sample of 5 tests for one suite. describe is used to write a group of tests called Test Suite.

test and it uses to write a single test case.

demo.test.js:

describe("Demo test suite", () => {
  test("Tests1", () => {
    expect(120).toBeGreaterThan(100);
  });

  it("Test2", () => {
    expect(2).toBe(2);
  });

  it("Test3", () => {
    expect(10).toBe(10);
  });
  it("Test4", () => {
    expect(11).toBe(11);
  });
  it("Test5", () => {
    expect(22).toBe(22);
  });
});

Next, Run the tests using one of the commands from a given

  • jest -i demo.test.js: running from command line
  • npm run test -- -i demo.test.js: It uses to run npm scripts which call jest CLI.

Here is a command output

B:\jest>npm run test -- -i demo.test.js

> [email protected] test
> node --experimental-modules --experimental-vm-modules node_modules/jest/bin/jest.js -i demo.test.js

(node:15040) ExperimentalWarning: VM Modules are an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PASS ./demo.test.js
Demo test suite
 Tests1 (3 ms)
 Test2 (1 ms)
 Test3 (1 ms)
 Test4 (1 ms)
 Test5

Test Suites: 1 passed, 1 total
Tests: 5 passed, 5 total
Snapshots: 0 total
Time: 1.178 s
Ran all test suites matching /demo.test.js/i.

From the given log output

  • One Test suite is passed
  • 5 tests are passed.

Now, Let’s see how to skip individual tests.

In Jest, You can skip test using test.skip function with adding skip modifier to the test function Another way to skip tests.

  • it.skip(“testname”,callbackfunction)
  • xit(“testname”,callbackfunction)
  • xtest(“testname”,callbackfunction)

Below is an example of code 4 tests are skipped and on test is active

describe("Demo test suite", () => {
  test("Tests1", () => {
    expect(120).toBeGreaterThan(100);
  });

  xtest("Test2", () => {
    expect(2).toBe(2);
  });

  test.skip("Test3", () => {
    expect(10).toBe(10);
  });
  it.skip("Test4", () => {
    expect(11).toBe(11);
  });
  xit("Test5", () => {
    expect(22).toBe(22);
  });
});

Output:

B:\jest>npm run test -- -i demo.test.js

> [email protected] test
> node --experimental-modules --experimental-vm-modules node_modules/jest/bin/jest.js -i demo.test.js

(node:18748) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PASS ./demo.test.js
Demo test suite
√ Tests1 (3 ms)
○ skipped Test2
○ skipped Test3
○ skipped Test4
○ skipped Test5

Test Suites: 1 passed, 1 total
Tests: 4 skipped, 1 passed, 5 total
Snapshots: 0 total
Time: 0.925 s, estimated 1 s
Ran all test suites matching /demo.test.js/i.

How to skip a group of tests suite using jest

describe is a function used to write a test suite that contains groups of tests.

You can use one of the below options

  • xdescribe()
  • describe.skip()

Here is an example

xdescribe("Demo test suite", () => {
  test("Tests1", () => {
    expect(120).toBeGreaterThan(100);
  });

  it("Test2", () => {
    expect(2).toBe(2);
  });

  it("Test3", () => {
    expect(10).toBe(10);
  });
  it("Test4", () => {
    expect(11).toBe(11);
  });
  it("Test5", () => {
    expect(22).toBe(22);
  });
});

Output:

B:\jest>npm run test -- -i demo.test.js

> [email protected] test
> node --experimental-modules --experimental-vm-modules node_modules/jest/bin/jest.js -i demo.test.js

(node:11648) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
Test Suites: 1 skipped, 0 of 1 total
Tests: 5 skipped, 5 total
Snapshots: 0 total
Time: 1.5 s
Ran all test suites matching /demo.test.js/i.

Jest it.only function run the example

If you have a group of tests, You want to run specific tests and ignore the remaining tests. You can use test.only() or it.only().

Added it.only() for Test3, Remaining tests are skipped.

describe("Demo test suite", () => {
  test("Tests1", () => {
    expect(120).toBeGreaterThan(100);
  });

  it("Test2", () => {
    expect(2).toBe(2);
  });

  it.only("Test3", () => {
    expect(10).toBe(10);
  });
  it("Test4", () => {
    expect(11).toBe(11);
  });
  it("Test5", () => {
    expect(22).toBe(22);
  });
});

Output:

B:\jest>npm run test -- -i demo.test.js

> [email protected] test
> node --experimental-modules --experimental-vm-modules node_modules/jest/bin/jest.js -i demo.test.js

(node:12660) ExperimentalWarning: VM Modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PASS ./demo.test.js
Demo test suite
 Test3 (2 ms)
 skipped Tests1
 skipped Test2
 skipped Test4
 skipped Test5

Test Suites: 1 passed, 1 total
Tests: 4 skipped, 1 passed, 5 total
Snapshots: 0 total
Time: 0.925 s, estimated 1 s
Ran all test suites matching /demo.test.js/i.

Jest Ignore Tests using CLI

Another option to ignore test files while running. jest cli has --testPathIgnorePatterns that contains patterns for test files to ignore execution.

Using npm scripts

npm run test -- --testPathIgnorePatterns="demo.test.js"

Using yarn command

yarn test --testPathIgnorePatterns="demo.test.js"

Using jest CLI

jest --testPathIgnorePatterns="demo.test.js"