Jest: Cannot read property 'addExpectationResult' of undefined

Created on 4 Apr 2018  路  18Comments  路  Source: facebook/jest

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

What is the current behavior?

In Jest 20, everything works fine. In Jest 21/22 I get this behavior. It happens on all computers I have attempted to test this project on. I have tried to remove node_modules and yarn.lock.

The tests run and fail and then this error is thrown in jest 22.

TypeError: Cannot read property 'addExpectationResult' of undefined
    at Env.fail (***/node_modules/jest-jasmine2/build/jasmine/Env.js:465:24)
    at next.fail (***/node_modules/jest-jasmine2/build/queue_runner.js:48:22)
    at <anonymous>

In Jest 21, this error is thrown:

TypeError: Cannot read property 'addExpectationResult' of undefined
    at Env.fail (***/node_modules/jest-jasmine2/build/jasmine/Env.js:510:24)
    at next.fail (***/node_modules/jest-jasmine2/build/queue_runner.js:42:22)
    at <anonymous>

The tests run with jest --runInBand --watch as they test database procedures. When I remove --watch, the error disappears, but the tests do not seem to run in sequence as they still fail.

A typical test file looks something like this:

import taskProvider from './somewhere'

const { db, seed } = global.testDependencies
let task

beforeEach(async () => {
  await db.migrate.latest()
  await seed()

  task = taskProvider({ db })
})

afterEach(async () => {
  await db.migrate.rollback()
})

afterAll(async () => {
  await db.destroy()
})

describe('something something', () => {
  it('somethings', async () => {
    await task({ foo: 'bar' })
    expect(await db.where({ foo: 'bar' }).length).not.toBe(0)
  })
})

What is the expected behavior?
Tests should run normally and no error should occur.

Please provide your exact Jest configuration

"jest": {
  "setupFiles": [
    "<rootDir>/testUtil/testSetup.js"
  ],
  "setupTestFrameworkScriptFile": "<rootDir>/testUtil/testFramework.js"
}

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

  System:
    OS: macOS High Sierra 10.13.3
    CPU: x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
  Binaries:
    Node: 8.9.2
    Yarn: 1.5.1
    npm: 5.5.1
  npmPackages:
    jest:
      wanted: ^22.4.3
      installed: 22.4.3
Needs More Info

Most helpful comment

FWIW I also ran into this and was led to this issue. Only happened in CI when an async test timed out but the underlying action continued until after the test suite finished (and I'm guessing the callback tried to add the result of the expectation). I could never get it to happen locally though.

All 18 comments

Can you set up a repo we can pull down and see the error in?

I will see what I can do. I'm quite swamped at work right now. I attempted to created a small repro, but could not reproduce it.

In a month I will definitely have the time to do it. In the mean time I'll just use v20.

Ah ok, @adrianhelvik please re-open when you have a chance to create repro repo and I'm happy to take a look

FWIW I also ran into this and was led to this issue. Only happened in CI when an async test timed out but the underlying action continued until after the test suite finished (and I'm guessing the callback tried to add the result of the expectation). I could never get it to happen locally though.

@patrickhulce were you able to fix this? I have the exact same behavior as you do. It doesn't happen on locally. Only on my Jenkins CI server.

@chris-fa only by fixing my failing test :) I was unsuccessful in creating a local repro for ya sorry!

@chris-fa I have the same issue. On local machines all test pass, but on our Virtual Machine some test cases fail with Failed: undefined. Did you find a solution to this?

@sibelius Tried it, by did not work. In the end updating NodeJS from 8.9.4 to 9.11.2 fixed it. Strange...

I just ran into this as well in 23

tejas:rideshare tejas.manohar$ cat node_modules/jest/package.json | jq .version
"23.6.0"

Sorry to comment on closed issuer, but this still happens if a long running call throws an error after the it has timed out...

@malixsys : Did you got any solution for the issue. I'm using jest 23.6.0 and I'm also getting the same issue.

Added a more explicit error in #8005.

@SimenB : As you mentioned "bah, node 6 and 8 doesn't have finally", if that is the root cause of the issue then the weird thing is, I've two repositories running in same jenkins server.
The unit test for both repository works fine in local. But in jenkins one repository works fine but in the other one I'm getting the above error during unit test.
Unfortunately it's not a public repository, so can't share it with you for more details.

That was just part of the changes in my PR, not related to any issue you're seeing.

@indranilatwork Just needed to go back and make sure that the test does not end before the callback...

@malixsys : The unit tests works fine for me in my local but not running in my jenkins server, it fails with the error:
`(node:7109) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'addExpectationResult' of undefined

(node:7109) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)

(node:7109) [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.

(node:7109) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'addExpectationResult' of undefined

(node:7109) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 7)

(node:7109) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'addExpectationResult' of undefined

(node:7109) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 8)
`

As I checked my unit test code, not seeing the callbacks are out of the test cases. I'm using jest 23.6.0

@indranilatwork These errors happen because your test is finished and THEN later some callback returns in your test code with an unhandled error...

Make sure the failing test returns all promises in your it() function and also that ALL fetch() or axios() calls are properly waited upon or return promises that are waited upon.

Your component should show that it is waiting for a call and your test should look for that sign to go away...

We use something like:

import { render, wait } from 'react-testing-library';
    it('renders properly', async () => {
        const { queryByTestId  } = render(
            <MyComp>
                {(loading) => loading ? (<p data-testid="loading">...</p>) : null}
            </MyComp>
        );
        await wait(() =>
            expect(queryByTestId('loading')).not.toBeInTheDocument()
        );
    });
Was this page helpful?
0 / 5 - 0 ratings