How to set Jest test retries globally

Viewed 96

Some of my test files in Jest are flaky. Identifying individual files and adding jest.retryTimes(2) to the top of those files helps. I would like to add that as a global setting rather than needing to add that line to the top of every file.

Is there a way to configure Jest so that it will always retry failed tests rather than needing to add that setting in each file?

Thanks!

1 Answers

You can use setupFilesAfterEnv:[array] in your jest config file https://jestjs.io/docs/configuration#setupfilesafterenv-array

Make a setup file (e.g setup-jest.js) and include the jest.retryTimes(2) in there.

// setup-jest.js

jest.retryTimes(2)


// jest.config.js

module.exports = {
  setupFilesAfterEnv: ['<rootDir>/setup-jest.js'],
  ...
};
Related