The Vercel AI SDK · Chapter 20 of 21

Testing with mock models

By the end of this chapter you can test Mali's code without calling a model: the chat route, a tool call and the answer after it, and the fallback middleware, each with a mock model you control. You will write Vitest tests with the SDK's MockLanguageModelV4, stream parts of your own with simulateReadableStream, script a model's steps with mockValues, and see how a Check judges a test: by breaking the code and watching the test fail. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Ready for customers
  • 3 tasks
  • TypeScript
  • In the all labs plan

The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.

Why you need this

Mali changes every day now. Deepa asks for a new rule in the instructions, Farhan adds a tool, Vikram moves a job to a cheaper model. Each change could break something that worked yesterday: the instructions dropped by mistake, a step limit removed, an error message leaking. Ananya wants to ship a change in the morning without holding her breath.

So far you have checked Mali by asking it things and reading the answers. That does not scale. A real model costs money for every test, is slow, needs the internet and a key, and does not answer the same way twice. A test that sometimes passes is worse than none. And some cases you cannot even make happen on demand: a provider that is down, a model that asks for a tool with exactly these arguments.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    route.test.ts: test the chat route with a mock model

    Write Vitest tests that give chatRoute (from ./server.ts, given) a MockLanguageModelV4 (from 'ai/test') and post a chat to the route. Test that: the answer streams as the mock's text-delta pieces; the model's first prompt message is { role: 'system', content: MALI_INSTRUCTIONS } and its last holds the customer's text (doStreamCalls[0].prompt); a mock whose doStream throws gives one error event whose errorText is SORRY.

    The Check runs your tests on the given route (all must pass, with no request to the lab model) and on broken copies of it (each must make a test fail).

  2. Task 2

    tools.test.ts: a tool call and the answer after it

    Write a test with a mock model that answers in two steps (mockValues from 'ai/test'): first a tool-call part for getOrder with the input '{"orderId":"HB-1042"}' and a finish with unified 'tool-calls', then a text answer. Post one message to chatRoute(model) and expect: two model calls, the second call's prompt ending with the tool's result (HB-1042 is out for delivery), and a stream with a tool-output-available event and the mock's answer.

  3. Task 3 · interview question

    Interview: fallback.test.ts, test the fallback

    Write tests for withFallback (from ./fallback.ts, given) with mock models and generateText (maxRetries: 0): a primary that throws an APICallError with statusCode 503 gives the backup's answer, with one backup call; a primary that throws a 400 makes generateText reject, with the backup never called; a working primary gives its own answer, the backup unused.

    Vikram will ask: these tests never call a real model. What can they not catch, and what would you do about that?

What you will learn

  • Mock models: MockLanguageModelV4 from ai/test, a model whose answers you write.
  • The model's side of a call: stream parts, finish reasons and usage as a provider gives them.
  • simulateReadableStream: a stream of parts, with delays if you want them.
  • What the model was sent: doStreamCalls and doGenerateCalls.
  • A model in steps: mockValues for a tool call, then an answer.
  • Good tests: tests that fail when the code breaks, and what mocks cannot catch.

More in Ready for customers

Build "Testing with mock models" yourself

HariBhari, a Bengaluru plant shop, wants Mali, a plant helper on its website. You build it end to end, from the first call to a chat that streams cards, asks before it adds to the cart and stays within budget. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of The Vercel AI SDK are free. This one comes with the all labs plan.