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

Cannot read property 'addExpectationResult' of undefined #5922

Closed
adrianhelvik opened this issue Apr 4, 2018 · 19 comments
Closed

Cannot read property 'addExpectationResult' of undefined #5922

adrianhelvik opened this issue Apr 4, 2018 · 19 comments

Comments

@adrianhelvik
Copy link

adrianhelvik commented Apr 4, 2018

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
@SimenB
Copy link
Member

SimenB commented Apr 4, 2018

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

@adrianhelvik
Copy link
Author

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.

@rickhanlonii
Copy link
Member

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

@patrickhulce
Copy link

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.

@christopher-francisco
Copy link

christopher-francisco commented Jun 5, 2018

@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.

@patrickhulce
Copy link

patrickhulce commented Jun 5, 2018

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

@yss14
Copy link

yss14 commented Aug 2, 2018

@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
Copy link

sibelius commented Aug 2, 2018

this is fixed here #6794

@yss14
Copy link

yss14 commented Aug 3, 2018

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

@tejasmanohar
Copy link

I just ran into this as well in 23

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

@malixsys
Copy link

malixsys commented Feb 8, 2019

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

@indranilatwork
Copy link

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

@SimenB
Copy link
Member

SimenB commented Feb 28, 2019

Added a more explicit error in #8005.

@indranilatwork
Copy link

@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.

@SimenB
Copy link
Member

SimenB commented Mar 4, 2019

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

@malixsys
Copy link

malixsys commented Mar 4, 2019

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

@indranilatwork
Copy link

@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

@malixsys
Copy link

malixsys commented Mar 5, 2019

@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()
        );
    });

@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

10 participants