vitest/prefer-mock-return-shorthand
January 2, 2026 ยท View on GitHub
๐ Prefer mock return shorthands.
๐ง This rule is automatically fixable by the --fix CLI option.
When working with mocks of functions that return simple values, vitest provides some API sugar functions to reduce the amount of boilerplate you have to write.
These methods should be preferred when possible.
The following patterns are warnings
vi.fn().mockImplementation(() => 'hello world')
vi.spyOn(fs.promises, 'readFile').mockImplementationOnce(() =>
Promise.reject(new Error('oh noes!')),
)
myFunction
.mockImplementationOnce(() => 42)
.mockImplementationOnce(() => Promise.resolve(42))
.mockReturnValue(0)
The following patterns are not warnings
vi.fn().mockResolvedValue(123)
vi.spyOn(fs.promises, 'readFile').mockReturnValue(
Promise.reject(new Error('oh noes!')),
)
vi.spyOn(fs.promises, 'readFile').mockRejectedValue(new Error('oh noes!'))
vi.spyOn(fs, 'readFileSync').mockImplementationOnce(() => {
throw new Error('oh noes!')
})
myFunction
.mockResolvedValueOnce(42)
.mockResolvedValueOnce(42)
.mockReturnValue(0)