jest: Cannot read property 'addExpectationResult' of undefined

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 4
  • Comments: 19 (4 by maintainers)

Most upvoted comments

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.

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

Added a more explicit error in #8005.

I just ran into this as well in 23

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