Skip to content

proposal: add shorthand rule for throwing mock implementations #901

Description

@nami8824

Summary

Vitest 4.1 added mockThrow and mockThrowOnce as shorthand APIs for mocks that throw:

mock.mockThrow(new Error('error message'))
mock.mockThrowOnce(new Error('first call error'))

Currently, eslint-plugin-vitest has shorthand rules for returned values and promises:

  • prefer-mock-return-shorthand
  • prefer-mock-promise-shorthand

However, throwing mock implementations are still not covered.

This proposes adding a new rule, tentatively named prefer-mock-throw-shorthand, that reports mockImplementation / mockImplementationOnce callbacks whose only behavior is throwing a value.

Examples

Invalid:

vi.fn().mockImplementation(() => {
  throw new Error('error message');
});

mock.mockImplementationOnce(() => {
  throw new Error('error message');
});

Valid:

vi.fn().mockThrow(new Error('error message'));

mock.mockThrowOnce(new Error('error message'));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions