Skip one test in test file Jest

80,974

Solution 1

I found the answer here

https://devhints.io/jest

test('it is raining', () => {
  expect(inchesOfRain()).toBeGreaterThan(0);
});

test.skip('it is not snowing', () => {
  expect(inchesOfSnow()).toBe(0);
});

Link on off doc

Solution 2

You can also exclude test or describe by prefixing them with an x.

Individual Tests

describe('All Test in this describe will be run', () => {
  xtest('Except this test- This test will not be run', () => {
   expect(true).toBe(true);
  });
  test('This test will be run', () => {
   expect(true).toBe(true);
  });
});

Multiple tests inside a describe

xdescribe('All tests in this describe will be skipped', () => {
 test('This test will be skipped', () => {
   expect(true).toBe(true);
 });

 test('This test will be skipped', () => {
   expect(true).toBe(true);
 });
});

Solution 3

Skip a test

If you'd like to skip a test in Jest, you can use test.skip:

test.skip(name, fn)

Which is also under the following aliases:

  • it.skip(name, fn) or
  • xit(name, fn) or
  • xtest(name, fn)

Skip a test suite

Additionally, if you'd like to skip a test suite, you can use describe.skip:

describe.skip(name, fn)

Which is also under the following alias:

  • xdescribe(name, fn)

Solution 4

Run Only A Subset of Tests:

If you are debugging/writing/extending a test in a test suite which has many tests you just need to run the one you are working on but adding .skip to everyone can be painful.

So .only comes to the rescue. You can optionally skip others during testing using .only flag, it can be super useful for big suites where you need to add or debug a test.

describe('some amazing test suite', () => {
  test('number one', () => {
    // some testing logic
  }
  test('number two', () => {
    // some testing logic
  }
  ...
  test('number x', () => {
    // some testing logic
  }
  test.only('new test or the one needs debugging', () => {
    // Now when you run this suite only this test will run
    // so now you are free to debug or work on business logic
    // instead to wait for other tests to pass every time you run jest
    // You can remove `.only` flag once you're done!
  }
}

You can also add .only to more than one tests in a test suite or file if you want to run multiple in conjunction!

Share:
80,974
Gleichmut
Author by

Gleichmut

Here is my professional profile where you can know more about me https://www.linkedin.com/in/dmkazakov

Updated on July 21, 2022

Comments

  • Gleichmut
    Gleichmut almost 2 years

    I'm using Jest framework and have a test suite. I want to turn off/skip one of my tests.

    Googling documentation doesn't give me answers.

    Do you know the answer or source of information to check?

  • ptim
    ptim almost 5 years
    ...and test.only()
  • anhquan
    anhquan over 4 years
    thanks, i find this solution is simple and practical.
  • oyeyipo
    oyeyipo over 3 years
    describe.skip() for tests suits
  • Bilal Shafi
    Bilal Shafi over 2 years
    Addition: .only works with it() as well, just like .skip
  • marko kraljevic
    marko kraljevic about 2 years
    it simply doesnt work with yarn test, jest is very annoying