Jest: TypeError: Cannot read property 'addExpectationResult' of undefined

Created on 3 Apr 2018  ·  2Comments  ·  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?

Bug

What is the current behavior?

When an it block returns a promise, but then there's an uncaught exception before the promise resolves, an internal jest error occurs.

If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

it('should foo', function () {
    setImmediate(() => {
        throw new Error('argh');
    });
    return new Promise((resolve, reject) => setTimeout(() => reject(new Error('foo')), 10));
});

When this test is run with jest it results in the following output:

$ jest --config jest.config.js myTest.js 
(node:27482) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'addExpectationResult' of undefined
(node:27482) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 FAIL  myTest.js
  ✕ should foo (14ms)

  ● should foo

    argh

      1 | it('should foo', function () {
      2 |     setImmediate(() => {
    > 3 |         throw new Error('argh');
      4 |     });
      5 |     return new Promise((resolve, reject) => setTimeout(() => reject(new Error('foo')), 10));
      6 | });

      at Immediate.setImmediate (myTest.js:3:15)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.281s, estimated 1s
Ran all test suites matching /myTest.js/i.

A bit of poking around reveals that the error comes from here: https://github.com/facebook/jest/blob/f014d92f1a16c19ca18a2d9023f1fb148149aa38/packages/jest-jasmine2/src/jasmine/Env.js#L556

What is the expected behavior?

That the internal jest error doesn't appear. Ideally I think I would like both the uncaught exception and the promise rejection to be rendered.

Please provide your exact Jest configuration

module.exports = {
    rootDir: require('path').resolve(__dirname, '../..'),
    testEnvironment: 'node',
    testPathIgnorePatterns: ['/node_modules/'],
    collectCoverageFrom: ['**/*.js', '!(test|coverage|node_modules)/**']
};

Run npx envinfo --preset jest in your project directory and paste the
results here

npx: installed 1 in 1.568s

  System:
    OS: Linux 4.13
    CPU: x64 Intel(R) Core(TM) i7-5600U CPU @ 2.60GHz
  Binaries:
    Node: 8.9.3
    Yarn: Not Found
    npm: 5.6.0
  npmPackages:
    jest:
      wanted: 23.0.0-alpha.4
      installed: 23.0.0-alpha.4

Also happens with 22.4.3

Help Wanted Needs Triage

Most helpful comment

PR very welcome adding an if around currentRunnable() and throwing a better error

All 2 comments

We should improve the error here (saying something like "error caught after test environment was torn down"). I think the underlying issue is the same as #5311 though (we remove error handlers between specs), so I'll close this as a dupe of that

PR very welcome adding an if around currentRunnable() and throwing a better error

Was this page helpful?
1 / 5 - 1 ratings