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

Negated form of prefer-to-have-been-called-once can be confusing

Open geoffswift opened this issue 9 months ago • 0 comments

With the rule prefer-to-have-been-called-once, this code...

expect(mySpy).not.toHaveBeenCalledTimes(1);

can be (correctly) autofixed as follows...

expect(mySpy).not.toHaveBeenCalledOnce();

However there seems to be a common misconception that not.toHaveBeenCalledOnce() means the method was never called. I can see that arguably it reads that way (in English), and perhaps for that reason we should not be encouraging not.toHaveBeenCalledOnce().

I have found many instances where people write not.toHaveBeenCalledOnce() when their intent was not.toHaveBeenCalled() or equivalently toHaveBeenCalledTimes(0).

Worse case scenario this potentially leads to false positive test passes.

I'd be pleased to see an option at least to allow the autofix for the negated case to be disabled. I have set my repo up to ban not.toHaveBeenCalledOnce() using jest/no-restricted-matchers, since we found every single instance of this to be a mistake.

geoffswift avatar Feb 14 '25 00:02 geoffswift