Skip to content
GitHub

createMock

function in meocord/testing Since 4.0.0

createMock<T extends object>(props?: MockProps<T>): DeepMocked<T>

Creates a mock of any type, with no class needed, for service doubles and interfaces.

Use it for a provider a testing module injects in place of the real one, or for a discord.js type the other factories do not build. For a discord.js class, createMockInteraction keeps its prototype.

Parameters

NameTypeDescription
props?MockProps<T>

Values the mock returns as given, such as the data the code reads; see MockProps.

Returns

DeepMocked<T>

Examples

TypeScript
import { expect } from 'vitest'

interface Mailer {
  send(to: string, text: string): Promise<boolean>
}
const mailer = createMock<Mailer>()
mailer.send.mockResolvedValue(true)

await mailer.send('ada', 'Welcome!')
expect(mailer.send).toHaveBeenCalledWith('ada', 'Welcome!')