DEV Community

Jonas Schumacher
Jonas Schumacher

Posted on

11 2

In what order does Jest execute tests anyway? (And how to change it!)

Jest will execute different test files potentially in parallel, potentially in a different order from run to run. Per file, it will run all describe blocks first and then run tests in sequence, in the order it encountered them while executing the describe blocks.

Here's an illustration of that per file behavior, copied straight out of the doc link above:

describe('outer', () => {
  console.log('describe outer-a');

  describe('describe inner 1', () => {
    console.log('describe inner 1');
    test('test 1', () => {
      console.log('test for describe inner 1');
      expect(true).toEqual(true);
    });
  });

  console.log('describe outer-b');

  test('test 1', () => {
    console.log('test for describe outer');
    expect(true).toEqual(true);
  });

  describe('describe inner 2', () => {
    console.log('describe inner 2');
    test('test for describe inner 2', () => {
      console.log('test for describe inner 2');
      expect(false).toEqual(false);
    });
  });

  console.log('describe outer-c');
});

// describe outer-a
// describe inner 1
// describe outer-b
// describe inner 2
// describe outer-c
// test for describe inner 1
// test for describe outer
// test for describe inner 2
Enter fullscreen mode Exit fullscreen mode

If you want to run files in sequence as well, run Jest with the --runInBand command line flag. (-i does the same thing.) Doing this, the scheduler might still run them in a different order from run to run. To prevent this as well, you will need to define your own test sequencer.

To sort test files alphabetically for example, create a file with the following contents:

// testSequencer.js
const Sequencer = require('@jest/test-sequencer').default;

class CustomSequencer extends Sequencer {
  sort(tests) {
    // Test structure information
    // https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
    const copyTests = Array.from(tests);
    return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
  }
}

module.exports = CustomSequencer;
Enter fullscreen mode Exit fullscreen mode

Then, set the following in your Jest config:

// jest.config.js
{
  "testSequencer": "path/to/testSequencer.js"
}
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (3)

Collapse
 
pmckeown profile image
Paul McKeown • Edited

In your config example, the filename should be // jest.config.json

Collapse
 
ifeanyi_okafor_7a59fbc526 profile image
Ifeanyi Okafor

Does this exist on typescript?

Collapse
 
ifeanyi_okafor_7a59fbc526 profile image
Ifeanyi Okafor

I have tried everything to get this on typescript but it keeps failing

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay