[prefer-expect-assertions] Addition of an "ifPromisesUsed" option
See original GitHub issueI really like the prefer-expect-assertions
rule, but requiring a test file to be covered in either expect.assertions(n)
or expect.hasAssertions()
calls when assertions aren’t nested inside promises gets a little messy.
It would be rad to be able to have an ifPromisesUsed
option on the rule that when enabled, would only throw the rule if:
- The test case returns a Promise directly to Jest and
- There are no
expect()
calls inside of the Promises resolve or reject methods.
test('should not trigger the rule because there is explicit expectation by itself', () => {
expect(true).toBeTruthy();
});
test('should trigger the rule because there is an expectation inside of a Promise resolver', () => {
expect.hasAssertions(); // expect.assertions(1);
return methodThatShouldAPromise(done => {
expect(true).toBeTruthy();
done();
});
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Expect - Jest
The expect function is used every time you want to test a value. You will rarely call expect by itself. Instead, you will...
Read more >Best Way to Test Promises in Jest - Stack Overflow
expect.assertions(number) verifies that a certain number of assertions are called during a test. This is often useful when testing asynchronous ...
Read more >Jest Expect - w3resource
expect.extend can be used to add your own matchers to Jest. ... If the promise is rejected, the assertion will fail.
Read more >Expect · Jest
The argument to expect should be the value that your code produces, and any argument to the matcher should be the correct value....
Read more >Demystifying Jest Async Testing Patterns | by Liran Tal - Medium
Expecting Assertions · Your tests are synchronous · You are using promises (in which case, just return the expectation) · You are using...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@erunion
re the burden of writing
expect.hasAssertions
boilerplate, you can use asetupFilesAfterEnv
script:https://github.com/G-Rath/babel-plugin-replace-ts-export-assignment/blob/master/test/setupExpectEachTestHasAssertions.ts
https://github.com/G-Rath/babel-plugin-replace-ts-export-assignment/blob/8dfdca32c8aa428574b0cae341444fc5822f2dc6/package.json#L52-L54
Granted you can’t use that if you’re using tools other than
expect
, but that’ll do a lot of what you want, and you can invert it by addingexpect(true).toBe(true)
at the top of tests that would otherwise fail, if you wanted.I completely agree, but it’s the conditions that I’m not convinced we can accurately match enough of w/o edge cases and generally ending up attempting to do the job of a static analyzer - that’s why it’s generally best practice to always stick
expect.hasAssertions
in your tests if possible, regardless of complexity.The focus of my review will be the complexity of the implementation vs what it actually covers, and what it doesn’t cover (i.e “if you import
methodThatShouldAPromise
then this option bails out”).None of this is meant to discourage you from making a PR if you want to have a go; I just want to share some of my thoughts & concerns 🙂
There are 2 use cases for
expect.hasAssertions();
- one is the promises one you point out, and the other is conditional logic (if
orthrow
). All these cases are better covered by https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/valid-expect-in-promise.md, https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-try-expect.md and https://github.com/jest-community/eslint-plugin-jest/blob/master/docs/rules/no-if.md. If you use those rules, you won’t needprefer-expect-assertions
as the cases it tries to guard against should be covered.That said, I’m not against the option you’re suggesting. @G-Rath thoughts?