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

fail does not work when testing code using a try/catch #663

Open
thehale opened this issue Oct 9, 2023 · 0 comments · May be fixed by #664
Open

fail does not work when testing code using a try/catch #663

thehale opened this issue Oct 9, 2023 · 0 comments · May be fixed by #664

Comments

@thehale
Copy link

thehale commented Oct 9, 2023

Bug

  • package version: 4.0.1
  • node version: 18.18.0
  • yarn version: 1.22.19

Relevant code or config

// my.test.js
function invokeWithTry(callback) {
  try {
    return callback();
  } catch (err) {
    return err;
  }
}


test("fail works in a try/catch", () => {
  const result = invokeWithTry(() => expect().fail("expected failure")); // Erroneously passes
  expect(result).toBeUndefined(); // Actually fails here b/c `result` is a `JestAssertionError`
});
// package.json
{
  "devDependencies": {
    "jest": "^29.7.0",
    "jest-extended": "^4.0.1"
  },
  "jest" : {
    "setupFilesAfterEnv": ["jest-extended/all"]
  }
}

What you did:

I passed a callback containing expect().fail() to a function that invokes the callback in a try/catch block.

What happened (please provide anything you think will help):

The failure was not registered since the catch intercepted the JestAssertionError raised by the fail matcher.

Reproduction repository (if possible): https://github.com/thehale/jest-extended-fail-not-working-in-try-catch

@thehale thehale linked a pull request Oct 9, 2023 that will close this issue
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant