Genkit JS API reference
    Preparing search index...

    Type Alias MockModel

    MockModel: ModelAction & {
        lastRequest: GenerateRequest | undefined;
        lastRequestMessage: Message | undefined;
        lastRequestText: string | undefined;
        requestCount: number;
        requests: GenerateRequest[];
        toolResponses: { name: string; output: unknown; ref?: string }[];
        reset(): void;
        respondWith(respond: MockRespond): void;
    }

    A mock model with typed inspection of the calls it received. The extra members are read-only views over the recorded calls.

    Type Declaration

    • ReadonlylastRequest: GenerateRequest | undefined

      The request from the most recent call, or undefined if never called.

    • ReadonlylastRequestMessage: Message | undefined

      The final message of the most recent request, wrapped as a Message so you can read it the same way you read a response — .text, .media, .toolRequests, etc. undefined if the model was never called.

      assert.match(model.lastRequestMessage!.text, /Summarize: long text/);
      
    • ReadonlylastRequestText: string | undefined

      The full assembled conversation of the most recent request, flattened to a single string (system + every message, in order). Use it for prompt- assembly assertions on any mock — including ones returning structured output, where echoModel can't be used. undefined if never called.

      assert.match(model.lastRequestText!, /system: Be terse/);
      
    • ReadonlyrequestCount: number

      How many times this model was called.

    • Readonlyrequests: GenerateRequest[]

      Every request this model received, oldest first.

    • ReadonlytoolResponses: { name: string; output: unknown; ref?: string }[]

      The tool results fed back to the model in the most recent request, in order. Use it to assert which tools ran and what they returned, without digging through message content yourself. Empty if the model was never called or saw no tool results.

      assert.deepStrictEqual(model.toolResponses.map((t) => t.name), ['lookup']);
      
    • reset: function
      • Clears recorded history (requests, requestCount, …) and restores the respond behavior given at construction, re-arming a queued respond from its first item. Call it in beforeEach so tests sharing a mock stay order-independent.

        Returns void

    • respondWith: function
      • Replaces the respond behavior for subsequent calls. Accepts everything MockModelOptions.respond does; an array re-arms as a fresh queue. Recorded history is untouched — use MockModel.reset for that.

        Together with reset() this supports the register-once idiom: define the mock once per test file, then give each test its own behavior.

        const model = mockModel(ai, { name: 'menuModel' });
        beforeEach(() => model.reset());

        test('...', async () => {
        model.respondWith({ text: 'scripted' });
        // ...
        });

        Parameters

        Returns void