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,
): ExecutionContextBuilds 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
| Name | Type | Description |
|---|---|---|
controller | C | 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 |
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 |
Returns
ExecutionContextThe context for that call.
Examples
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)