Skip to content
GitHub

createMockInteraction

function in meocord/testing Since 4.0.0

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

createMockInteraction<T extends object>(
  Class: InteractionClass<T>,
  props: Omit<MockProps<WithCache<T, 'raw'>>, 'options' | 'channel'> & {
    options?: object
    guildId: string
    member: APIInteractionGuildMember
    channel?: never
  },
): DeepMocked<WithCache<T, 'raw'>>

createMockInteraction<T extends object>(
  Class: InteractionClass<T>,
  props: MockProps<WithCache<T, CacheType>> &
    OutsideServer &
    NotRaw<WithCache<T, CacheType>>,
): 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

NameType
ClassInteractionClass<T>

The discord.js class to mock.

props?MockProps<T> & NotRaw<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()
TypeScript
import { expect } from 'vitest'

const command = createMockInteraction(ChatInputCommandInteraction, { commandName: 'status' })
await command.reply({ content: 'Starting.' })
const before = await command.fetchReply()
const after = await command.editReply({ content: 'Done.' })
// A new message object, as discord.js gives: the same message by id, and the one held before still says what it said
expect(after).not.toBe(before)
expect(after.id).toBe(before.id)
expect(before.content).toBe('Starting.')
TypeScript
import { expect } from 'vitest'

// Under useStrictMocks(): a command that collects a click on its reply
@Controller()
class PickController {
  @Command('pick', CommandType.SLASH)
  async pick(interaction: ChatInputCommandInteraction) {
    const response = await interaction.reply({ content: 'Pick one.', withResponse: true })
    const collector = response.resource!.message!.createMessageComponentCollector({ max: 1 })
    collector.on('collect', click => click.update({ content: `Picked ${click.customId}.` }))
  }
}

const module = MeoCordTestingModule.create({ controllers: [PickController] }).compile()
const command = createMockInteraction(ChatInputCommandInteraction, { commandName: 'pick' })
await module.dispatch(command)
const click = createMockInteraction(ButtonInteraction, { customId: 'red', message: await command.fetchReply() })
await module.dispatch(click)
expect(getResponse(click).calls.map(call => call.method)).toEqual(['update'])

Parameters

NameType
ClassInteractionClass<T>

The discord.js interaction class to mock.

propsOmit< MockProps<WithCache<T, 'raw'>>, 'options' | 'channel' > & { options?: object guildId: string member: APIInteractionGuildMember channel?: never }

The interaction's properties, with the raw member and the server's guildId; see MockProps.

props.options?object
props.guildIdstring
props.memberAPIInteractionGuildMember
props.channel?never

Returns

DeepMocked<WithCache<T, 'raw'>>

Examples

TypeScript
import { expect } from 'vitest'

const command = createMockInteraction(ChatInputCommandInteraction, { guildId: '100000000000000001', member: createMockRawMember() })
expect(command.inRawGuild()).toBe(true)

Parameters

NameType
ClassInteractionClass<T>

The discord.js interaction class to mock.

propsMockProps< WithCache<T, CacheType> > & OutsideServer & NotRaw<WithCache<T, CacheType>>

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)