Giter VIP home page Giter VIP logo

jest-retry-all-hooks's Introduction

jest-retry-all-hooks

Patch beforeAll and afterAll hooks to work with jest.retryTimes.

Motivation

jest.retryTimes(1);

beforeAll(() => {
  // This is a part of a root or a nested describe block, and it is not retried normally.
});

test('test', () => {
  // The tests and their `beforeEach` and `afterEach` hooks are retried.
});

afterAll(() => {
  // This is a part of a root or a nested describe block, and it is not retried normally.
});

So, what does this library do? It converts beforeAll and afterAll hooks to smart beforeEach and afterEach hooks, which usually run only once, but if a test fails with an error, here is what happens:

jest.retryTimes(1);

beforeEach(() => {
  // if never ran before, run a function passed to `beforeAll`
  // if previous test failed, run that function again
});

test('test', () => {
  // The behavior `test`, `beforeEach`, `afterEach` stays the same.
});

afterEach(() => {
  // if the test function or preceeding hooks failed, run a function passed to `afterAll`
});

afterAll(() => {
  // if never ran before, run a function passed to `afterAll`
});

See the tests/hooks.ts for an elaborate example of how it works:

B b -1 a A
B b -2 a A
B b  3 a
  b  1 a
  b -2 a A
B b  2 a A

The example above demonstrates how jest.retryTimes(2) will work with the patched beforeAll (B), beforeEach (b), afterEach (a), and afterAll (A) hooks and flaky tests (1, 2, 3), where negative numbers indicate failures, and positive numbers indicate successes.

Please note that the patched beforeAll and afterAll hooks won't be associated with describe blocks, so they will be executed whenever a test fails, even if the afterAll hook seems to be outside the describe block.

Installation

Install the package:

npm install --save-dev jest-retry-all-hooks

Add to your jest.config.js:

- testEnvironment: 'node',
+ testEnvironment: 'jest-environment-emit/node',
+ testEnvironmentOptions: {
+   eventListeners: [
+     'jest-retry-all-hooks',
+   ],
+ },

If you don't have jest-enviroment-emit installed, you can install it:

npm install --save-dev jest-environment-emit

You can also use any other test environment compatible with jest-environment-emit, e.g.:

Use GitHub Topic Search to find more compatible test environments.

License

Licensed under the MIT License.

jest-retry-all-hooks's People

Contributors

noomorph avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.