Skip to main content

Command Palette

Search for a command to run...

JEST Modifiers

Published
View as Markdown
  1. Introduction
    In the Jest testing framework, "modifiers" refer to specific methods that change the behavior or scope of a test. These modifiers help in organizing and managing tests by allowing you to control when and how certain tests are run.
    By using these modifiers, you can control the execution flow and organization of your tests, making your testing process more efficient and manageable.

  2. .only
    Use .only to run only the specified test or test suite. This is useful when you want to focus on a particular test without running the entire test suite.

     test.only('this is the only test that will run', () => {
       expect(true).toBe(true);
     });
    
     describe.only('this is the only describe block that will run', () => {
       test('test within this describe block', () => {
         expect(true).toBe(true);
       });
     });
    
  3. .skip
    Use .skip to skip a particular test or test suite. This is useful when a test is not ready or temporarily disabled.

     test.skip('this test will be skipped', () => {
       expect(true).toBe(true);
     });
    
     describe.skip('this describe block will be skipped', () => {
       test('test within this describe block', () => {
         expect(true).toBe(true);
       });
     });
    
  4. .todo
    Use .todo to mark a test as a "to-do". This indicates that the test is planned but not yet implemented.

     test.todo('this is a test that needs to be written');
    
  5. .each
    Use .each to run the same test with different sets of data. This is useful for data-driven testing.

     test.each([
       [1, 1, 2],
       [1, 2, 3],
       [2, 1, 3],
     ])('adds %i and %i to equal %i', (a, b, expected) => {
       expect(a + b).toBe(expected);
     });
    
  6. .concurrent
    Use .concurrent to run tests in parallel. This can speed up test execution by leveraging multiple CPU cores.

     test.concurrent('this test will run concurrently', async () => {
       const result = await someAsyncFunction();
       expect(result).toBe(true);
     });