eslint-plugin-jest icon indicating copy to clipboard operation
eslint-plugin-jest copied to clipboard

[expect-expect] Option to enforce the last statement of a test to be an assertion

Open CreativeTechGuy opened this issue 2 years ago • 3 comments

The expect-expect rule works great for identifying a test that doesn't have any assertions, but it doesn't work for identifying halfway-complete tests. Often more complex test cases (such as JSDOM tests) will have multiple assertions, usually before something has occurred to assert the base state is correct, then perform an action, and then assert that the values have changed as expected. I have found a few times where I'll be writing a test and get distracted halfway through and forget to finish the second half of the test. Code coverage looks good since all of the actions were performed, but the test was missing the critical assertions at the end.

In almost every test, there should be an assertion as the last statement. The only case where I can think of that not being true is when the assertions are inside of callback function, but that can be avoided by putting expect.assertions(number) as the last line.

Since it seems like a generally useful option which aligns with the spirit of this rule, I wanted to suggest this as a new feature.

CreativeTechGuy avatar Feb 01 '23 00:02 CreativeTechGuy

Code coverage looks good since all of the actions were performed, but the test was missing the critical assertions at the end.

Unhelpful drive-by suggestion: mutation testing can help on this front, e.g. https://stryker-mutator.io/

danielnixon avatar Feb 07 '23 04:02 danielnixon

@G-Rath thoughts on this? I think it sounds like a good idea - not sure if it should be an option on expect-expect or a separate rule

SimenB avatar Feb 18 '23 09:02 SimenB

@SimenB yeah it makes sense to me, and probably as an option to expect-expect 🤔

G-Rath avatar Feb 19 '23 18:02 G-Rath