Skip to content
GitHub

createMockInteraction

function in meocord/testing Since 4.0.0

createMockInteraction<T extends object>(
  Class: InteractionClass<T>,
  props?: MockProps<T>,
): DeepMocked<T>

createMockInteraction<T extends object>(
  Class: InteractionClass<T>,
  props: MockProps<WithCache<T, CacheType>> & OutsideServer,
): DeepMocked<WithCache<T, CacheType>>

Creates a mock instance of a discord.js class, such as an interaction, keeping its prototype so instanceof holds.

Use it for the interaction a test hands to invoke or dispatch. For a user, a message, a guild, a channel or a client, the dedicated factories fill in what discord.js would; for a type with no class, use createMock.

Parameters

NameTypeDescription
ClassInteractionClass<T>

The discord.js class to mock.

props?MockProps<T>

Values for properties the class declares readonly; see MockProps.

Returns

DeepMocked<T>

Examples

TypeScript
import { expect } from 'vitest'

const interaction = createMockInteraction(ButtonInteraction, { customId: 'ticket/42/close' })
expect(interaction.isButton()).toBe(true)
await interaction.reply({ content: 'Closed.' })
expect(interaction.replied).toBe(true)
await expect(interaction.reply({ content: 'Again.' })).rejects.toThrow()

Parameters

NameTypeDescription
ClassInteractionClass<T>

The discord.js interaction class to mock.

propsMockProps< WithCache<T, CacheType> > & OutsideServer

The interaction's properties, with guild: null, member: null or a DM channel; see MockProps.

Returns

DeepMocked<WithCache<T, CacheType>>

Examples

TypeScript
import { expect } from 'vitest'

const command = createMockInteraction(ChatInputCommandInteraction, { channel: createMockChannel(DMChannel), guild: null })
expect(command.inGuild()).toBe(false)