jest/prefer-to-have-been-called-times Style
What it does
In order to have a better failure message, toHaveBeenCalledTimes should be used instead of directly checking the length of mock.calls.
Why is this bad?
This rule triggers a warning if toHaveLength is used to assert the number of times a mock is called.
Examples
Examples of incorrect code for this rule:
js
expect(someFunction.mock.calls).toHaveLength(1);
expect(someFunction.mock.calls).toHaveLength(0);
expect(someFunction.mock.calls).not.toHaveLength(1);Examples of correct code for this rule:
js
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);This rule is compatible with eslint-plugin-vitest, to use it, add the following configuration to your .oxlintrc.json:
json
{
"rules": {
"vitest/prefer-to-have-been-called-times": "error"
}
}How to use
To enable this rule using the config file or in the CLI, you can use:
json
{
"plugins": ["jest"],
"rules": {
"jest/prefer-to-have-been-called-times": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
plugins: ["jest"],
rules: {
"jest/prefer-to-have-been-called-times": "error",
},
});bash
oxlint --deny jest/prefer-to-have-been-called-times --jest-plugin