Skip one test in test file Jest

Javascriptnode.jsTestingJestjs

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

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 - Javascript

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 - Javascript

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGleichmutView Question on Stackoverflow
Solution 1 - JavascriptGleichmutView Answer on Stackoverflow
Solution 2 - JavascriptSeth McClaineView Answer on Stackoverflow
Solution 3 - JavascriptYuciView Answer on Stackoverflow
Solution 4 - JavascriptBilal ShafiView Answer on Stackoverflow