Skip to content
GitHub

inspectHandler

function in meocord/testing Since 4.1.0

inspectHandler<C extends new (...args: any[]) => unknown>(
  controller: C,
  methodName: keyof InstanceType<C> & string,
  options?: InspectHandlerOptions,
): HandlerInspection

Reports what runs when a handler is dispatched, and the metadata declared on it, without running anything.

Use it to check that decorators set a handler up as intended: its guards with their params, its interceptors, filters and cooldowns, in the order dispatch applies them, and its @Defer. Under useStrictMocks(), the guards, interceptors and observers are those dispatch runs for the handler's context types: a stage declared with types is listed only for a handler of one of them, and an autocomplete lists no interceptors. Default mode lists every one, with a warning once where dispatch runs fewer. To check what a handler does, run it with TestingModule.invoke.

Parameters

NameType
controllerC

The controller class declaring the handler.

methodName(keyof InstanceType<C>) & string

The handler method's name.

options?InspectHandlerOptions

app, to include the global stages and observers @MeoCord declares.

options.app?new (...args: any[]) => unknown

The @MeoCord app class: its global guards and interceptors come before the handler's own, its global filters are tried after them, and its observers are listed.

Returns

HandlerInspection

The handler's stages in order, its @Defer options, its message pattern, and readers for its metadata.

Examples

TypeScript
import { expect } from 'vitest'

@Controller()
@UseGuard(StaffGuard)
class ModerationController {
  @Command('ban', CommandType.SLASH)
  @UseGuard({ provide: ChannelGuard, params: { channelIds: ['123456789012345678'] } })
  async ban(interaction: ChatInputCommandInteraction) {
    await respond(interaction).send('Banned.')
  }
}
const { guards } = inspectHandler(ModerationController, 'ban')
expect(guards).toEqual([StaffGuard, { provide: ChannelGuard, params: { channelIds: ['123456789012345678'] } }])