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(...).rejects.toEqual() crashes test runner when assertion fails #6633

Closed
harukogaki opened this issue Jul 5, 2018 · 2 comments
Closed

Comments

@harukogaki
Copy link

🐛 Bug Report

I am attempting to write a test that asserts that an error thrown conforms to a particular format. Ive noticed that the .rejects.toEquals approach works assuming the assertions pulls through. However, if it fails the test runner crashes. Ive tried putting try catch blocks over the assertion but to no avail. I also did some light reading into the issue and found that this has been a known issue so I'm looking for potential mitigation strategies or guidance.

To Reproduce

const promiseThatRejects = new Promise((resolve,reject) => reject("Error"))
expect(promiseThatRejects).rejects.toEqual("Error2")

Expected behavior

the test runner doesn't crash

Actual behavior

/app/node_modules/react-scripts/scripts/test.js:20
  throw err;
  ^

Error: expect(received).toEqual(expected)

Expected value to equal:
  "Error2"
Received:
  "Error"
    at Object.<anonymous> (/app/node_modules/jest-matchers/build/index.js:176:51)
    at Generator.throw (<anonymous>)
    at step (/app/node_modules/jest-matchers/build/index.js:1:287)
    at /app/node_modules/jest-matchers/build/index.js:1:486
    at <anonymous>
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
@SimenB
Copy link
Member

SimenB commented Jul 6, 2018

using reject (or resolves) makes the matcher async, so you need to wait for the promise. Either by await or returning it.

https://repl.it/@SimenB/BriskDarkgreyRectangle

@SimenB SimenB closed this as completed Jul 6, 2018
batmat added a commit to batmat/evergreen that referenced this issue Oct 16, 2018
ESLint rules for Jest.

Stumbled into it from jestjs/jest#6633 (comment)
batmat added a commit to batmat/evergreen that referenced this issue Oct 17, 2018
ESLint rules for Jest.

Stumbled into it from jestjs/jest#6633 (comment)
@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
None yet
Projects
None yet
Development

No branches or pull requests

2 participants