-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Labels
p2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)pr welcome
Description
Clear and concise description of the problem
Currently, when using mock factories, the expected return type is any. This makes it possible to return broken mocks that do not comply with the original module's signature.
// @filename: src/basic.ts
export const squared = (n: number) => n * n
// @filename: test/mocks.test.ts
import { expect, test, vi } from 'vitest';
test('Mocked module with broken contract', async () => {
vi.doMock(import('../src/basic.js'), () => ({
// When not manually typing the result, we can return a broken mock from the factory, as its expected return type is `any`
sqrd: () => 1,
}));
// This throws the following error: Error: [vitest] No "squared" export is defined on the "../src/basic.js" mock. Did you forget to return it from "vi.mock"?
const { squared } = await import('../src/basic.js');
expect(squared(2)).toBe(1);
});Suggested solution
It would be great if the mock factory's return type was expected to be the same as the original module's, instead of using any.
Alternative
Meanwhile, we've got hundreds of return type overrides in our codebase for each mock with satisfies Awaited<ReturnType<typeof importOriginal>> to ensure the mock's type is correct, but this is tedious and error-prone.
// @filename: src/basic.ts
export const squared = (n: number) => n * n
// @filename: test/mocks.test.ts
import { expect, test, vi } from 'vitest';
test('Mocked module', async () => {
vi.doMock(
import('../src/basic.js'),
(importOriginal) =>
// This ensures the value returned from the factory is compatible with the original module.
// I would like for the whole `satisfies` statement to not be necessary
({ squared: () => 1 } satisfies Awaited<
ReturnType<typeof importOriginal>
>)
);
const { squared } = await import('../src/basic.js');
expect(squared(2)).toBe(1);
});Additional context
Here's a reproduction: https://stackblitz.com/edit/vitest-dev-vitest-fkhseh?file=test%2Fmocks.test.ts
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
Metadata
Metadata
Assignees
Labels
p2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)pr welcome