Skip to content

Latest commit

 

History

History
44 lines (28 loc) · 1.17 KB

File metadata and controls

44 lines (28 loc) · 1.17 KB

vitest/prefer-to-have-been-called-times

📝 Suggest using toHaveBeenCalledTimes().

⚠️ This rule warns in the 🌐 all config.

🔧 This rule is automatically fixable by the --fix CLI option.

In order to have a better failure message, toHaveBeenCalledTimes should be used instead of directly checking the length of mock.calls.

Rule details

This rule triggers a warning if toHaveLength is used to assert the number of times a mock is called.

Note

This rule should ideally be paired with prefer-to-have-length

The following patterns are considered warnings:

expect(someFunction.mock.calls).toHaveLength(1)
expect(someFunction.mock.calls).toHaveLength(0)

expect(someFunction.mock.calls).not.toHaveLength(1)

The following patterns are not warnings:

expect(someFunction).toHaveBeenCalledTimes(1)
expect(someFunction).toHaveBeenCalledTimes(0)

expect(someFunction).not.toHaveBeenCalledTimes(0)

expect(uncalledFunction).not.toBeCalled()

expect(method.mock.calls[0][0]).toStrictEqual(value)