-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Support failing a test if no assertions are ran #2209
Comments
Jest 17.1 (today?) will support |
That is an awesome - I'd like every // Should fail because no assertions
it("does nothing", () => {
})
// could encourage this for stubs, which can pass
it("does nothing")
/// should fail because no assertions happen during the test
const fs = require("fs")
it("does something with the file", () => {
fs.readFile("file.js", (err, data) => {
expect(data).toBeTruthy()
})
}) |
Hmm, this makes sense but we do have a legit use case here of making sure something doesn't fail so I don't wanna make it more restrictive.
|
There's |
The default test that create-react-app ships with doesn't have an assertion: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/src/App.test.js#L7 Jest also doesn't enforce usage of cc @gaearon in case he has an opinion here. |
I use But maybe some work can be put into https://www.npmjs.com/package/eslint-plugin-jest getting it closer to https://www.npmjs.com/package/eslint-plugin-ava, which has a lot of assertions for making sure you use the API properly? (Started migrating to jest today, and the only thing I'm really missing from Ava is the Eslint plugin (and no globals, but that is more of a style, it doesn't really matter)) |
Yeah, I agree. I think between I didn't realize there was an eslint plugin for Jest. We should consider merging this into Jest, making it official and expanding it's scope a lot; including an optional option to make sure "expect" is used at least once within a test. @SimenB are you interested in working on this? @jkimbo would you be interested to work more on this eslint plugin and helping to make it official? |
I'll happily contribute to the eslint plugin! |
That sounds great. To be honest, I haven't considered this myself before, so I'm gonna rely on ideas from you guys. What do you want it to look like and lint against? I can help figure out what the right defaults should be, though :) I care most about finding patterns that guide people to write tests with good quality and rules that are helpful rather than annoying. cc @zertosh who always has opinions on linting. |
@cpojer I am very happy in making it more official! Anything I can do to help let me know and always happy to accept pull requests. |
That's great! Would you be willing to move it into the "packages" folder of the Jest repo and add me to the project on npm? |
This thread went perfectly 👍 |
Awesome! |
One thing I really like about tap, is that you must either use |
That's one of the things I dislike about tap... 😆 |
All done! |
To get back to this, it could be checked statically as well. https://github.com/tlvince/eslint-plugin-jasmine/blob/master/docs/rules/missing-expect.md The issue in the OP is mitigated by #3067 though 😄 |
@cpojer - would you accept a PR adding I use |
No, Jest 20 has |
Is there a way to make every |
No. You can use the |
That's expected, but not on purpose (same thing as |
Got it. It would be nice if a capability like this were to be part of the API. I genuinely want to assert that at least one assertion is called |
One issue with the For instance, given a suite like the following: describe("concerning assertions being made", () => {
afterEach(() => expect.hasAssertions())
it("fails when no assertions are made", () => {})
it("does not check assertions are made when an exception is thrown", () => {
throw new Error("only care about this error")
expect(true).toEqual(true)
})
}) The output looks like:
The last reported error about missing assertions is unnecessarily noisy. It would be nice to be able to do something like the following: afterEach(spec => !spec.hasFailed && expect.hasAssertions()) Although I do understand wanting to keep details about the currently running spec hidden to the test file, so perhaps something like this could be considered? afterEachSuccess(() => expect.hasAssertions()) |
Actually, the following situation also leads to this noisy reporting, so this is a more general problem with it("does not check assertions are made when an exception is thrown", () => {
expect.hasAssertions()
return new Promise(() => {
throw new Error("only care about this error")
}).then(() => expect(true).toEqual(true))
}) |
wdyt @SimenB? |
We should probably count a failing assertion as an assertion before rethrowing it, or something like that |
Is there any interest from the Jest maintainers for a It seems like an optional setting in the config should help those of us who could really benefit from it. Working with large teams, makes it kind of difficult to enforce that everyone uses More than happy to help if that change would be welcomed. |
Wouldn't it fit better as an eslint rule? |
Probably yes (although there may be some cases where lint doesn't quite get it right, such as using a helper that does the |
The static analysis answer is popular in this issue report, but it's missing the forrest for the trees. The crux of the problem Is that static analysis via ESLint is sub-par to catch runtime issues. As an example, the documented "correct" use of it('should work with callbacks/async', () => {
somePromise().then(res => expect(res).toBe('passed'));
}); Yes, there is an expect here, but this expect should never execute as the promise is not returned. I/we use these rules all the time, but they do not catch logic issues with non-trivial tests. Which is why we need a broader runtime solution. If the answer to a global opt-in for this is a plain "no" then it is what is it is. What would be an alternative option? There are test runners which provide this config (ava) but switching is not an option. I've been looking into setupFilesAfterEnv is that a possible solution for extending the functionality to support this globally? Maybe via a plugin of some kind? From where I'm standing it seems like we just need a way to assert that a test finished executing and did not trigger assertions and fail that test. But again, without an explicit |
[EDIT : removed noise] |
Following up on this one more time. @mpacary thanks for following, but this isn't what I need. After much trial and error, and debugging some unsavory bugs I have fallen back to patching the Jest API to do the thing I wanted (and what the issue is asking for). As I speculated before, I had to add a "plugin" package of sorts to perform runtime checks for missing assertions in tests. See the plugin here - https://github.com/ballercat/jest-plugin-must-assert I pointed that thing at a repo with 10k+ tests and found some interesting results. For example, any conditional assertions are dangerous(async or not) and result in false positives. For example
... is a super common mistake to make. It's a huge bummer too, since no async logic is even involved. If the My final two cents: Some of the hoops one has to jump through to get this sort of feedback from Jest/test-runner is a bit extreme. I had to for example, use the Zone.js library to ensure Promises/setTimeouts did not register assertions after a test has completed. I would love for the Jest team to consider some way to improve the integration of assertion library with the actual test runner. The global nature of Thanks for everyones help! |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
A bad translation from mocha -> Jest had tests that looked like this:
Which passes, but shouldn't.
This can also happen with promises and any async code being tested.
If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal repository on GitHub that we can
npm install
andnpm test
.What is the expected behavior?
Offer a config variable that fails your test if no assertions are used during the test run. IMO default to on too.
Run Jest again with
--debug
and provide the full configuration it prints. Please mention your node and npm version and operating system.N/A - but was running on master, node 7, npm3.
The text was updated successfully, but these errors were encountered: