-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Closed
Labels
p2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)
Description
Clear and concise description of the problem
As a developer using vitest I want an easy way to create mock instances of a class. While I can us vi.mock('module') to automock the entire file I have to jump thru hoops to get a mock instance, mainly to avoid TypeScript errors
import { Class } from 'module'
vi.mock('module')
// @ts-ignore-error Ignore constructor argument errors
const mockInstance = new Class()
vi.mock will mock the whole module, so if you wanted another exported value to use the real implementation you need a factory to import the real implementation.
Suggested solution
import { Class } from 'module'
import { vi } from 'vitest'
const mock = vi.mockInstance(Class)
Alternative
No response
Additional context
No response
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
p2-nice-to-haveNot breaking anything but nice to have (priority)Not breaking anything but nice to have (priority)
Type
Projects
Status
Approved