This guide explains how to test error processors with `MockLanguageModelV2`, including simulating API errors, verifying retry behavior, and constructing complete stream events. It outlines happy-path, control, and selectivity tests, shows how to seed tool-call messages, and provides commands for running focused or broader test suites.
88%
Does it follow best practices?
Run evals on this skill
Adds up to 20 points to the overall score
Passed
No findings from the security scan
The canonical home for this skill is testing-core-processors in mastra-ai/mastra
How to write integration tests for error processors in packages/core/src/processors/.
@mastra/core and @internal/test-utils from sourceUse MockLanguageModelV2 from @internal/ai-sdk-v5/test to simulate API errors and verify retry behavior.
import { APICallError } from '@internal/ai-sdk-v5';
import { convertArrayToReadableStream, MockLanguageModelV2 } from '@internal/ai-sdk-v5/test';
// Track calls and captured prompts
let callCount = 0;
const receivedPrompts: any[] = [];
const model = new MockLanguageModelV2({
doGenerate: async ({ prompt }) => {
callCount++;
receivedPrompts.push(JSON.parse(JSON.stringify(prompt)));
if (callCount === 1) {
throw new APICallError({
message: '...',
url: '...',
requestBodyValues: {},
statusCode: 400,
responseBody: '...',
isRetryable: false,
});
}
return {
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { inputTokens: 10, outputTokens: 20, totalTokens: 30 },
content: [{ type: 'text', text: 'response' }],
warnings: [],
};
},
doStream: async ({ prompt }) => {
// Same error logic as doGenerate
// IMPORTANT: Stream response must include all event types:
return {
rawCall: { rawPrompt: null, rawSettings: {} },
warnings: [],
stream: convertArrayToReadableStream([
{ type: 'stream-start', warnings: [] },
{ type: 'response-metadata', id: 'id-0', modelId: 'mock-model', timestamp: new Date(0) },
{ type: 'text-start', id: 'text-1' },
{ type: 'text-delta', id: 'text-1', delta: 'response text' },
{ type: 'text-end', id: 'text-1' },
{ type: 'finish', finishReason: 'stop', usage: { inputTokens: 10, outputTokens: 20, totalTokens: 30 } },
]),
};
},
});The stream mock format requires stream-start, response-metadata, text-start, text-delta, text-end, and finish events. Using only text-delta + finish (the minimal format) will result in empty text output because the AI SDK expects the full event sequence. See prefill-error-recovery.test.ts for the reference pattern.
For each error processor, write at minimum:
agent.generate() succeeds, mock called 2x, retry prompt has expected modificationsagent.generate() throws the expected errorWhen seeding conversation history for tool-related tests, pass messages as the second argument to agent.generate() or agent.stream() using the AI SDK message format:
const messages = [
{ role: 'user', content: 'Do something' },
{ role: 'assistant', content: [{ type: 'tool-call', toolCallId: 'some-id', toolName: 'myTool', args: {} }] },
{ role: 'tool', content: [{ type: 'tool-result', toolCallId: 'some-id', toolName: 'myTool', result: 'done' }] },
];
await agent.generate(messages);# Run focused processor tests
npx vitest run packages/core/src/processors/my-processor.test.ts
# Run all processor tests
npx vitest run packages/core/src/processors/
# Full core test suite (slower)
pnpm test:coreNone for mock-based integration tests. For live API tests:
ANTHROPIC_API_KEY — for testing against real Anthropic APIOPENROUTER_API_KEY — for testing provider switching scenariosmastra-ai/mastra
7f5453c
npxnpx vitestnpx vitest runpnpmpnpm test:corevitestCanonical home
since Sep 4, 2026
If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.