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

toThrow does not behave like toEqual for non-errors #7692

Closed
rubennorte opened this issue Jan 24, 2019 · 3 comments · Fixed by #7697
Closed

toThrow does not behave like toEqual for non-errors #7692

rubennorte opened this issue Jan 24, 2019 · 3 comments · Fixed by #7697
Assignees
Labels
Milestone

Comments

@rubennorte
Copy link
Contributor

rubennorte commented Jan 24, 2019

🐛 Bug Report

It seems like last changes in toThrow (#7621) broke a behaviour uses were relying on in Jest 23.

We can expect toThrow to behave like toEqual on the thrown error, but that's not the case anymore.

To Reproduce

Steps to reproduce the behavior:

Run this test with Jest 23 and Jest 24.0.0-alpha.13:

class CustomError { // not extending Error!
  constructor(message) {
    this.message = message;
  }
}

function throwError() {
  throw new CustomError('foo');
}

test('toThrow', () => {
  expect(throwError).toThrowError(new CustomError('foo'));
});

test('toEqual', () => {
  try {
    throwError();
  } catch (e) {
    expect(e).toEqual(new CustomError('foo'));
    return;
  }

  throw new Error('Expected error here');
});

Expected behavior

No errors. Those two tests should be equivalent.

Also, I found the error message a bit confusing:

screen shot 2019-01-24 at 10 45 23

@SimenB
Copy link
Member

SimenB commented Jan 24, 2019

(before anyone picks this up, @pedrottimark is working on it)

@rubennorte
Copy link
Contributor Author

I could repro this asymmetry with non-errors, but there might be more cases like that.

@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.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants