Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'addExpectationResult' of undefined #5911

Closed
papandreou opened this issue Apr 2, 2018 · 3 comments
Closed

Comments

@papandreou
Copy link

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

@SimenB
Copy link
Member

SimenB commented Apr 23, 2018

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

@SimenB
Copy link
Member

SimenB commented Apr 23, 2018

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

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants