Skip to content
GitHub

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

NameTypeSinceDescription
overrides?MockMessageOverrides4.1.0

The message's content, components and the rest; see MockMessageOverrides.

overrides.id?string4.1.0

The message's id.

overrides.content?string4.1.0

The message's text.

overrides.author?User4.1.0

Who sent it, such as a user from createMockUser, or the client's own user for a message the bot sent; a new person otherwise. Two messages from one author count as one user's, such as for a per-user cooldown. It is cached on the message's client, and in a server its member is the guild's cached member for that user, or a new one.

overrides.components?readonly ( | APIMessageTopLevelComponent | JSONEncodable<APIMessageTopLevelComponent> )[]4.1.0

Its top-level components: action rows, or Components V2 such as a container.

overrides.embeds?readonly ( APIEmbed | JSONEncodable<APIEmbed> )[]4.1.0

Its embeds.

overrides.flags?MessageFlagsResolvable4.1.0

Its flags: a number, flag names or a MessageFlagsBitField.

overrides.guild?Guild | null4.1.0

The guild it was sent in, such as one from createMockGuild with members in its cache; null for a DM.

overrides.channel?TextBasedChannel | DeepMocked<BaseChannel>4.1.0

The channel it was sent in, such as one from createMockChannel. Unless given, a text channel of its guild, cached there, or the author's DM channel for a DM. A channel given sets the message's guild, unless guild is given too: none for a DM channel, so inGuild() is false, and the channel's own server for a server's channel. One in another server than the guild given is refused, naming both. The channel is cached on the message's client, as the gateway caches it.

overrides.client?Client4.1.0

The client it arrived on, such as one from createMockClient; a new mock client otherwise.

overrides.users?readonly User[]4.1.0

Users in the client's users.cache, by their id, beside those the content mentions.

overrides.editedTimestamp?number | null4.1.0

When it was last edited, in milliseconds since the epoch; null, the default, for a message never edited.

Returns

DeepMocked<Message> & OmitPartialGroupDMChannel<Message> & { deleted: boolean }

Examples

TypeScript
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)