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

expect().toBe() crashes silently when failing called from an async callback #3519

mo opened this issue May 8, 2017 · 6 comments


Copy link

mo commented May 8, 2017

using jest 20.0.0, the following test crashes silently on the expect().toBe() and thus fails to call done() so it ends up hitting the jasmine default timeout. If you put a try/catch around the expect().toBe() line you can see that the error it crashes on (and fails to print) is that it cannot compare undefined to "hello" because they have different types. Also note that if you uncomment line 6 and comment out the setTimeout() line, effectively converting the asynchronous callback into a synchronous callback; then jest correctly prints the error immediately and doesn't hit the timeout. I'm expecting jest to properly report the error, even for the asynchronous callback case.

const isCurrentlyBuggy = true;

function sometimesBuggyFunc(callback) {
  if (isCurrentlyBuggy) {
    // If callback is invoked synchronously jest correctly reports: undefined != 'foo'

    // If callback if invoked asynchronously jest crashes silently on the
    // expect().toBe() and thus never calls done() causing the test case
    // to hang until jasmine default timeout is reached.
    setTimeout(() => callback(undefined), 0);
  } else {

test.only('the thing', done => {
  sometimesBuggyFunc(actual => {
Copy link

You need to catch the error and call This is how it works :(
However this is not an issue, if you're returning a promise from test.
See docs on that:

Copy link

@mo I just ran into this as well. I ended up making myself a little helper function:

function makeCallback(done, body) {
  return (...args) => {
    try {
    } catch (error) {;

Using this, you'd replace your code above with:

sometimesBuggyFunc(makeCallback(done, actual => {

Copy link

I get this error when running any test marked as async.
eg if I run:
it('this test times out without telling me why', async () => { expect(1).toBe(2); });
I get the following failure:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

  102 | function createdPatchedSpec(OriginalSpec, registry) {
  103 |   function PatchedSpec(attrs) {
> 104 |     OriginalSpec.apply(this, arguments);
      |                  ^
  105 |     if (attrs && {
  106 |       registry[] = this;
  107 |     }

I'm running jest 24.1

Copy link

I get this error when running any test marked as async.

I'm running jest 24.1


Copy link

SimenB commented Mar 10, 2019

Can you open up a new issue with a reproduction?

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.
None yet
None yet

No branches or pull requests

6 participants