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: Omit<MockProps<WithCache<T, 'raw'>>, 'options'> & {
options?: object
guildId: string
member: APIInteractionGuildMember
},
): DeepMocked<WithCache<T, 'raw'>>
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
| Name | Type | Description |
|---|---|---|
Class | InteractionClass<T> | The discord.js class to mock. |
props? | MockProps<T> | Values for properties the class declares |
Returns
DeepMocked<T>Examples
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
| Name | Type | Description |
|---|---|---|
Class | InteractionClass<T> | The discord.js interaction class to mock. |
props | Omit<
MockProps<WithCache<T, 'raw'>>,
'options'
> & {
options?: object
guildId: string
member: APIInteractionGuildMember
} | The interaction's properties, with the raw |
props.options? | object | |
props.guildId | string | |
props.member | APIInteractionGuildMember |
Returns
DeepMocked<WithCache<T, 'raw'>>Examples
import { expect } from 'vitest'
const command = createMockInteraction(ChatInputCommandInteraction, { guildId: '100000000000000001', member: createMockRawMember() })
expect(command.inRawGuild()).toBe(true)Parameters
| Name | Type | Description |
|---|---|---|
Class | InteractionClass<T> | The discord.js interaction class to mock. |
props | MockProps<
WithCache<T, CacheType>
> &
OutsideServer | The interaction's properties, with |
Returns
DeepMocked<WithCache<T, CacheType>>Examples
import { expect } from 'vitest'
const command = createMockInteraction(ChatInputCommandInteraction, { channel: createMockChannel(DMChannel), guild: null })
expect(command.inGuild()).toBe(false)