-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Clear and concise description of the problem
Vitest has great auto-mocking/auto-spying algorithms, which can be used like so:
vi.mock(import("some/module/path")) // functions do nothing and return undefined
vi.mock(import("some/module/path"), { spy: true }) // functions work normally, but can be manipulated and asserted against
If you want to customize a mocked module, you can do it like so:
vi.mock(import("some/module/path"), async (importOriginal) => {
const original = await importOriginal()
const mocked = vi.mockObject(original)
return {
...mocked,
counter: vi.fn(() => original.counter() + 1)
}
})
But it is impossible to customize a spied-on module the same way, since there is no spy-version of vi.mockObject()
.
Suggested solution
vi.mockObject(original, { spy: true })
Note: The function already supports this in its implementation (of course, as it's the one used by vi.mock()
):
vitest/packages/mocker/src/browser/mocker.ts
Lines 118 to 121 in c1f78d2
public mockObject( | |
object: Record<string | symbol, any>, | |
moduleType: MockedModuleType = 'automock', | |
): Record<string | symbol, any> { |
vitest/packages/mocker/src/registry.ts
Line 156 in c1f78d2
export type MockedModuleType = 'automock' | 'autospy' | 'manual' | 'redirect' |
It's just not exposed to the end-users.
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.