Skip to content

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

Open
@thehale

Description

@thehale

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions