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

Closed
mo opened this issue May 8, 2017 · 6 comments
Closed

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

mo opened this issue May 8, 2017 · 6 comments

Comments

@mo
Copy link
Contributor

@mo 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'
    //callback(undefined);

    // 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 {
    callback('hello');
  }
}

test.only('the thing', done => {
  expect.assertions(1);
  sometimesBuggyFunc(actual => {
    expect(actual).toBe('foo');
    done();
  });
});
@thymikee
Copy link
Collaborator

@thymikee thymikee commented May 10, 2017

You need to catch the error and call done.fail(). This is how it works :(
However this is not an issue, if you're returning a promise from test.
See docs on that: http://facebook.github.io/jest/docs/en/asynchronous.html#promises

@thymikee thymikee closed this May 10, 2017
@randycoulman
Copy link

@randycoulman randycoulman commented Jun 16, 2017

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

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

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

sometimesBuggyFunc(makeCallback(done, actual => {
  expect(actual).toBe('foo');
}));
@justincrow
Copy link

@justincrow justincrow commented Feb 18, 2019

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 && attrs.id) {
  106 |       registry[attrs.id] = this;
  107 |     }

I'm running jest 24.1

@terencewestphal
Copy link

@terencewestphal terencewestphal commented Mar 8, 2019

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

I'm running jest 24.1

Same

@SimenB
Copy link
Collaborator

@SimenB SimenB commented Mar 10, 2019

Can you open up a new issue with a reproduction?

@github-actions
Copy link

@github-actions github-actions bot commented May 12, 2021

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.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants