Skip to main content

Testing

Testing language models can be challenging, because they are non-deterministic and calling them is slow and expensive. To enable you to unit test your code that uses the AI SDK, the AI SDK Core includes mock providers and test helpers. You can import the following helpers from ai/test:
  • MockEmbeddingModelV3: A mock embedding model using the embedding model v3 specification.
  • MockLanguageModelV3: A mock language model using the language model v3 specification.
  • mockId: Provides an incrementing integer ID.
  • mockValues: Iterates over an array of values with each call. Returns the last value when the array is exhausted.
You can also import simulateReadableStream from ai to simulate a readable stream with delays. With mock providers and test helpers, you can control the output of the AI SDK and test your code in a repeatable and deterministic way without actually calling a language model provider.

Examples

You can use the test helpers with the AI Core functions in your unit tests:

generateText

streamText

generateText with Output

embed

embedMany

Simulate UI Message Stream Responses

You can also simulate UI Message Stream responses for testing, debugging, or demonstration purposes. Here is a Next.js example: