npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-retry-all-hooks

v1.0.1

Published

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

Downloads

3,938

Readme

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.