Skip to content

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

Open
@geoffswift

Description

@geoffswift

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions