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

[new rule] prefer `.not.toHaveBeenCalled()` over `toHaveBeenCalledTimes(0)`

Open ge65cer opened this issue 2 years ago • 3 comments

// instead of
expect(testSpy).toHaveBeenCalledTimes(0)

// prefer
expect(testSpy).not.toHaveBeenCalled()

// unaffected
expect(testSpy).toHaveBeenCalledTimes(1)
expect(testSpy).not.toHaveBeenCalledTimes(5)

ge65cer avatar Aug 17 '22 09:08 ge65cer

~Could you please provide a description of what you'd like, including examples of correct and incorrect code that this rule would check?~ thanks!

G-Rath avatar Aug 17 '22 10:08 G-Rath

I'm not sure if enough of a clear gain here to make it worth having a whole new rule for this (the failure message is very similar, and it could be more consistent depending on the surrounding code), given you could use no-restricted-syntax to enforce this using CallExpression[callee.property.name='toHaveBeenCalledTimes'][arguments.length=1][arguments.0.value=0] 🤔

G-Rath avatar Aug 19 '22 19:08 G-Rath

Reviving this as I'm seeing a lot of areas of this happening within a codebase - would be nice to have an autofixable rule here instead of flagging restricted syntax.

park-jemin avatar Dec 04 '23 14:12 park-jemin