jest: [jest-circus] missing fail() method
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:
- Open a JS project with jest >= 27.0.0
- Write a test that includes a
fail()
method call - 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
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 98
- Comments: 29 (1 by maintainers)
Commits related to this issue
- update example to use different jest-expect-message issue with new jest https://github.com/facebook/jest/issues/11698 https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/55803 https://git... — committed to lolopinto/ent by lolopinto 3 years ago
- update example to use different jest-expect-message issue with new jest https://github.com/facebook/jest/issues/11698 https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/55803 https://git... — committed to lolopinto/ent by lolopinto 3 years ago
- cleanup * update to latest @snowtop/ent * update to latest jest * remove uses of fail in test as it doesn't seem it's being fixed anytime soon per https://github.com/DefinitelyTyped/DefinitelyTyped/d... — committed to lolopinto/ent by lolopinto 3 years ago
- fix(tests): Replaced fail by throw Error It seems that fail() is broken and won't be fixed: https://github.com/jestjs/jest/issues/11698 — committed to e-Learning-by-SSE/nm-skill-lib by Elscha 7 months ago
As a result of this issue, there is currently a discrepancy between
@types/jest
, which does definefail
, andjest-circus
, which does not definefail
. Discussion in DefinitelyTypedAs a temporary workaround, you can define your own
fail
function:Hi! How this incredibly stupid bug could still be present after a so long time?! Iāve never seen a test framework without a fail() method in my whole life. This not fixed regression after 2 years is a huge embarrassment for jest and a huge lack of serious.
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.
Unfortunately thatās not equivalent. The problem Iām having is that I need to fail a test from a location where any
throw
will be caught. Is there any more equivalent option available?Any chance this will be fixed in any new release?
Using the answer proposed here I tested if the same behavior could be applied to Jest. My theory was correct. It is possible to fail a Jest test if you call the done callback function with some param. Here an example:
Output:
ā this test must fail
Any update on this?
Or at least some information as to:
fail
Same here! Would love to have this issue alleviated sooner than later š
Same here, too. No change after fix
Same here
Hi, just wanted to share the workaround Iām using. I have created a fail function using
expect
and a failing comparison. It also displays messages in an okayish way. Itās not the cleanest solution, but it solves the problem mentioned here. Maybe it is helpful for someone.Donāt take me wrong, but they donāt owe you or us anything. The developers have their own lives, with their own problems and need to pay their bills at the end of the month too. If you really need this, you can use your spare time (like they do when they develop jest) and submit a PR to fix this issue.
This is still happening in 29.5.0
Hellā¦ Iāve just arrived here to find a regression from >2.5 years ago and itās full of workarounds.
Will someone either close this as wonāt fix or fix it!
Thatās what I will do now: by letting this shit for a so long time, they completely discredited themselves and showed their total lack of seriousness. I will replace jest by another framework for my current and next projects for all the companies, and hardly advise to everybody to never choose that framework for their tests. I already do the same with TypeOrm which proved its total lack of seriousness too about the non fixed regressions and bugs.
Or just move to another testing framework. I moved one project to vitest (Jest api compatible) this week, and noticed there was an expect.fail function.
After spending a few more hours on the problem this morning, I found a satisfactory solution using a custom matcher which builds off of the
fail()
matcher fromjest-extended
.Use a Custom Matcher (works in
try/catch
blocks)Example
There are already simple work-arounds like this in the thread above. Thereās also another way youāve missed that itās inferior to the original
fail
: it simply doesnāt work if itās executed within a callback where a parent performs acatch
. E.g.:In cases like this any
fail()
call incallback
will be silently ignored and the test still passes because all it does it throw an exception anyone can catch rather than actually mark the test as failed. Thatās a pretty big drawback.@vtgn: maybe because there are bigger and older issues with Jest, like #6695.
also running into this while trying to upgrade from jest 26 to jest 27ā¦
I just ran into a test where I was getting āfail() is undefinedā and had assumed all this time that fail worked like it used to since it exists in @types/jest.
Just to clarify why this functionality is important:
The above code with Jest 28 will now incorrectly always succeed, as
fail()
throw an exception that gets caught by thecatch
.We donāt want to catch any error either though, as unexpected errors should result in a test failure rather than success. We just want the tests to succeed when failures are expect.
I went ahead and created some test utility functions so I can continue using this pattern. I did end up finding and resolving a few more bugs too. Now the example test looks like:
It will be published on npm with
@dereekb/util@^8.1.0
.Iām not too familiar with the inner workings of Jest or why it dropped the previous functionality of
fail()
, but I imagine it could be brought back for the cases where we are looking for a specific error. Jestās it functionality could be extended with a function that looks for failures, (I.E. it.fail, or something) and then watch for a specific exception to be thrown that is thrown by failSuccessfully() or something to that manner. Someone more familiar with building Jest extensions may see a better way to implement it as an extension as well.