Vitest has a type helper vi.mocked to typed the passed in object as a mock object, it would be helpful if vietst-mock-extended has similar helper for mock and deepMock objects, a real world use case is shown below.
// @/libs/prisma.mock.ts
import { mockDeep } from "vitest-mock-extended";
import { PrismaClient } from "@/libs/prisma";
vi.mock(import("@/libs/prisma"), async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
prisma: mockDeep<PrismaClient>(),
};
});
// some.test.ts
import "@/libs/prisma.mock"; // Mock out the actual prisma client
import { prisma } from "@/libs/prisma";
const prismaMock = deepMockTypeHelper(prisma);
test("send notification", () => {
prismaMock.user.findUnique.mockResolvedValue(xxx);
...
}
A Vitest counterpart would be replace mockDeep<PrismaClient>() with vi.fn(), deepMockTypeHelper() with vi.mocked().
A quick but un-unified implementation would look like
import {
mock,
mockDeep,
mockFn,
} from "vitest-mock-extended";
export function deepMocked<T>(obj: T) {
return obj as ReturnType<typeof mockDeep<T>>;
}
export function mocked<T>(obj: T) {
return obj as ReturnType<typeof mock<T>>;
}
export function mockedFn<T>(obj: T) {
return obj as ReturnType<typeof mockFn<T>>;
}
The implementation is similar to vi.mocked but the devil is in the interface type definition (which I haven't found out the code)
Vitest has a type helper vi.mocked to typed the passed in object as a mock object, it would be helpful if vietst-mock-extended has similar helper for mock and deepMock objects, a real world use case is shown below.
A Vitest counterpart would be replace
mockDeep<PrismaClient>()withvi.fn(),deepMockTypeHelper()withvi.mocked().A quick but un-unified implementation would look like
The implementation is similar to vi.mocked but the devil is in the interface type definition (which I haven't found out the code)