Skip to content
GitHub

createExecutionContext

function in meocord/testing Since 4.1.0

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

Builds the ExecutionContext a guard, interceptor or filter receives for one handler, to test it on its own.

Use it for a stage that reads the handler's metadata, its params or the call's arguments, without building a module. The metadata is read from the real controller, as it is at runtime.

Parameters

NameTypeDescription
controllerC

The controller class declaring the handler.

methodName(keyof InstanceType<C>) & string

The handler method's name.

options?ExecutionContextOptions

The call's arguments, the stage's params, the handler's params and the call type.

options.args?unknown[]

The handler's arguments, such as the interaction and its params.

options.params?Record<string, unknown>

The params of the guard's { provide, params } entry, returned by getParams().

options.type?ExecutionContextType

What is being handled. Read from the first argument when omitted.

options.handlerParams?Record<string, unknown>

The handler's params, returned by getHandlerParams() and as getArgs()'s second argument, such as the validated and piped params an interceptor reads after next.handle(). Without it, args' second argument.

Returns

ExecutionContext

The context for that call.

Examples

TypeScript
import { expect } from 'vitest'

@Controller()
class StatsController {
  @Command('stats', CommandType.SLASH)
  async stats(interaction: ChatInputCommandInteraction) {
    await respond(interaction).send('Stats.')
  }
}
const interaction = createMockInteraction(ChatInputCommandInteraction)
const context = createExecutionContext(StatsController, 'stats', { args: [interaction] })
expect(context.getInteraction()).toBe(interaction)