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'));
Summary
Vitest 4.1 added
mockThrowandmockThrowOnceas shorthand APIs for mocks that throw:Currently,
eslint-plugin-vitesthas shorthand rules for returned values and promises:prefer-mock-return-shorthandprefer-mock-promise-shorthandHowever, throwing mock implementations are still not covered.
This proposes adding a new rule, tentatively named
prefer-mock-throw-shorthand, that reportsmockImplementation/mockImplementationOncecallbacks whose only behavior is throwing a value.Examples
Invalid:
Valid: