jest: beforeAll async hook hides errors

πŸ› Bug Report

The beforeAll hook hides errors.

To Reproduce

beforeAll(async () => {
    throw new Error('Y U NO SHOW?!')
});

Expected behavior

Errors thrown in beforeAll hooks should be thrown and not get swallowed up. Such a failure should also kill further testing for that file/parallel process given that whatever failed in beforeAll would be crucial for continued testing of that parallel.

Run npx envinfo --preset jest

Paste the results here:

npx: installed 1 in 0.736s

  System:
    OS: macOS 10.14.1
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
  Binaries:
    Node: 12.6.0 - ~/.nvm/versions/node/v12.6.0/bin/node
    npm: 6.9.0 - ~/.nvm/versions/node/v12.6.0/bin/npm
  npmPackages:
    jest: ^24.8.0 => 24.8.0 

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 57
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@lucasfcosta No. The issue here is that the errors thrown in beforeAll are getting swallowed, particularly when using an async function.

They come in as hidden unhandled promise exceptions instead.

I want to actually see the error that gets thrown in beforeAll when I’m using an async function as that would give an indicator as to the actual problem why the test parallel failed.

This is still unreleased/fixed. My dirty workaround:

// setup-after-env.js
const _beforeEach = global.beforeEach;
global.beforeEach = fn => {
  _beforeEach(async () => {
    try {
      await fn();
    } catch (e) {
      console.error(e);
      throw e;
    }
  });
};

EDIT: my current workaround is to use mocha + earl πŸ˜†

When you combine this issue with this one https://github.com/facebook/jest/issues/2441 Things get real fun

🀨

I added a repo to reproduce this, its not only a beforeAll issue. Its a general issue with rejected promises https://github.com/philiiiiiipp/jest-async-beforeall-error

Upgrading jest to the latest version fixed this issue for me (probably due to the new circus runner).

Cool πŸ˜ƒ

I tried it by changing the jest-jasmine2 ERROR_REGEX and now it shows perfectly fine. ( node v10.16.0 )

I also noticed that using jest-circus this works too. Which, I guess makes sense since it replaces jest-jasmine2.

Hi @philiiiiiipp, thanks for your reproducible, it was extremely helpful 😊

I managed to reproduce the error by running your example using Node v12.6 (as described by the issue’s author).

That error, however, only happens on Jest v24.8, not in the current state of master.

It’s possible to verify this by running yarn install and yarn test in your repo (which uses v24.8) and then running the same tests using the current version of master.

The reason why this happened is that _addMissingMessageToStack would not combine the message with the stack due to this regex.test check failing.

This has been fixed by adding a :? to the regex, here. The if condition for doing the replace also changed, but just changing the regex so that the test passes will be enough to fix this.

You can verify this by opening the node_modules file and changing jest-jasmine2/build/reporter.js to use the same value that is in master for ERROR_REGEX and then you will start seeing the promise rejection message.

This happened 26 days ago, so that’s why it’s not present in the last release.

Given the above I believe this issue can be closed.

Thanks again @philiiiiiipp for the reproducible πŸ’–