question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[jest-circus] missing fail() method

See original GitHub issue

This might be a known issue, but I could not find an existing issue so creating one here 😊 Also, I guess fail() was a bit of an undocumented feature, but we rely on it in our app for some nice developer experience improvements.

💥 Regression Report

After upgrading to Jest v27 (with jest-circus as default) the fail() method is no longer defined.

ReferenceError: fail is not defined

Last working version

Worked up to version: 26.6.3

Stopped working in version: 27.0.0

Can circumvent in 27.x with testRunner: "jest-jasmine2" in jest.config.js

To Reproduce

Steps to reproduce the behavior:

  1. Open a JS project with jest >= 27.0.0
  2. Write a test that includes a fail() method call
  3. Notice that any tests with a call to fail() might pass (depending on the structure), and you will see a “fail is not defined” error message in Jest v27 with jest-circus (works correctly with jest-jasmine2)

Expected behavior

Expected fail() to work by default, as before, without any changes to jest.config.js.

Link to repl or repo (highly encouraged)

See this repo for example of the regression: https://github.com/Darep/jest-circus-fail-method

Check the branch jasmine where the testRunner is changed and the tests run correctly 🙂

The repo also hilights the way we use fail(), just to give some background info & motivation from our use-case 😄

Run npx envinfo --preset jest

Paste the results here:

$ npx envinfo --preset jest
npx: installed 1 in 0.976s

  System:
    OS: macOS 11.4
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 14.16.1 - ~/n/bin/node
    Yarn: 1.21.1 - ~/n/bin/yarn
    npm: 6.14.12 - ~/n/bin/npm
  npmPackages:
    jest: ^27.0.6 => 27.0.6 

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:66
  • Comments:14

github_iconTop GitHub Comments

20reactions
srmaguracommented, Sep 18, 2021

As a result of this issue, there is currently a discrepancy between @types/jest, which does define fail, and jest-circus, which does not define fail. Discussion in DefinitelyTyped

As a temporary workaround, you can define your own fail function:

function fail(reason = "fail was called in a test.") {
  throw new Error(reason);
}

global.fail = fail;
13reactions
m-gagnoncommented, Sep 14, 2021

Any update on this? Many of my integration tests are missing the correct messaging now that this is undefined, and its causing a lot of confusion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

In Jest, how can I make a test fail? - Stack Overflow
Method-1. You can wrap your promise function within expect and tell jest the function should reject with the given error. If the someOperation() ......
Read more >
Configuring Jest
For example, with the following configuration jest will fail if there is less than 80% branch, line, and function coverage, or if there...
Read more >
Migration Guide | Detox
New Jest config​ ·.exports = { · maxWorkers: 1, · testEnvironment: './environment', · testRunner: 'jest-circus/runner', · testTimeout: 120000, · testRegex: '\\.e2e\\ ...
Read more >
How to use the jest-util.ErrorWithStack function in jest-util - Snyk
message = 'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.'; throw asyncError; ......
Read more >
jest | Yarn - Package Manager
‍ Developer Ready: Complete and ready to set-up JavaScript testing solution. Works out of the box for any React project. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found