createMockMessage
function in meocord/testing Since 4.0.0
createMockMessage(
overrides?: MockMessageOverrides,
): DeepMocked<Message> &
OmitPartialGroupDMChannel<Message> & { deleted: boolean }Creates a mock Message that tracks whether it has been deleted.
Use it for the message a message command reads, or the message a button sits on. Its author is a person, so
@MessageHandler does not skip it.
Parameters
| Name | Type | Since |
|---|---|---|
overrides? | MockMessageOverrides | 4.1.0 |
The message's content, components and the rest; see | ||
overrides.id? | string | 4.1.0 |
The message's id. | ||
overrides.content? | string | 4.1.0 |
The message's text. | ||
overrides.author? | User | 4.1.0 |
Who sent it, such as a user from | ||
overrides.components? | readonly (
| APIMessageTopLevelComponent
| JSONEncodable<APIMessageTopLevelComponent>
)[] | 4.1.0 |
Its top-level components: action rows, or Components V2 such as a container. Builders or API JSON, held as
discord.js's classes, such as | ||
overrides.embeds? | readonly (
APIEmbed | JSONEncodable<APIEmbed>
)[] | 4.1.0 |
Its embeds, builders or API JSON, held as discord.js's | ||
overrides.flags? | MessageFlagsResolvable | 4.1.0 |
Its flags: a number, flag names or a | ||
overrides.guild? | Guild | null | 4.1.0 |
The guild it was sent in, such as one from | ||
overrides.channel? | TextBasedChannel | DeepMocked<BaseChannel> | 4.1.0 |
The channel it was sent in, such as one from | ||
overrides.client? | Client | 4.1.0 |
The client it arrived on, such as one from | ||
overrides.users? | readonly User[] | 4.1.0 |
Users in the client's | ||
overrides.editedTimestamp? | number | null | 4.1.0 |
When it was last edited, in milliseconds since the epoch; | ||
Returns
DeepMocked<Message> & OmitPartialGroupDMChannel<Message> & { deleted: boolean }Examples
import { expect } from 'vitest'
const close = new ButtonBuilder().setCustomId('ticket/42/close').setLabel('Close').setStyle(ButtonStyle.Danger)
const message = createMockMessage({
content: 'Ticket #42',
components: [new ActionRowBuilder<ButtonBuilder>().addComponents(close)],
})
const click = createMockInteraction(ButtonInteraction, { customId: 'ticket/42/close', message })
await click.message.delete()
expect(message.deleted).toBe(true)