CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jest-fetch-mock

Comprehensive fetch API mocking solution for Jest testing environments with configurable responses and TypeScript support

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

response-mocking.mddocs/

Response Mocking

Core functionality for mocking successful HTTP responses with configurable bodies and options. Supports both one-time and persistent mocks with flexible response generation.

Capabilities

Mock Response

Sets up a mock response for all subsequent fetch calls until reset or changed.

/**
 * Mock response for all subsequent fetch calls
 * @param bodyOrFunction - Response body string or function that generates response
 * @param init - Optional response configuration (status, headers, etc.)
 * @returns FetchMock instance for chaining
 */
fetch.mockResponse(bodyOrFunction, init);

Usage Examples:

// Simple string response
fetch.mockResponse("Hello World");

// JSON response with status
fetch.mockResponse(JSON.stringify({ data: "test" }), { 
  status: 200,
  headers: { "Content-Type": "application/json" }
});

// Dynamic response function
fetch.mockResponse((request) => {
  if (request.url.includes("/api/users")) {
    return Promise.resolve(JSON.stringify({ users: [] }));
  }
  return Promise.resolve("Not found");
});

Mock Response Once

Sets up a mock response for the next fetch call only. Subsequent calls will use the default behavior or other configured mocks.

/**
 * Mock response for the next fetch call only
 * @param bodyOrFunction - Response body string or function that generates response
 * @param init - Optional response configuration (status, headers, etc.)
 * @returns FetchMock instance for chaining
 */
fetch.mockResponseOnce(bodyOrFunction, init);

Usage Examples:

// Mock single response
fetch.mockResponseOnce(JSON.stringify({ id: 1, name: "Alice" }));

// Chain multiple one-time responses
fetch
  .mockResponseOnce(JSON.stringify({ page: 1 }))
  .mockResponseOnce(JSON.stringify({ page: 2 }));

// With custom status and headers
fetch.mockResponseOnce("Server Error", { 
  status: 500,
  statusText: "Internal Server Error",
  headers: { "Content-Type": "text/plain" }
});

Once (Alias)

Convenient alias for mockResponseOnce providing the same functionality with shorter syntax.

/**
 * Alias for mockResponseOnce - mock response for the next fetch call only
 * @param bodyOrFunction - Response body string or function that generates response
 * @param init - Optional response configuration (status, headers, etc.)
 * @returns FetchMock instance for chaining
 */
fetch.once(bodyOrFunction, init);

Usage Examples:

// Same as mockResponseOnce
fetch.once(JSON.stringify({ success: true }));

// Chaining with alias
fetch
  .once("First response")
  .once("Second response")
  .once("Third response");

Mock Responses

Sets up multiple mock responses that will be used in sequence. Each response is used once, in the order provided.

/**
 * Mock multiple responses in sequence
 * @param responses - Array of responses (strings, arrays with [body, init], or functions)
 * @returns FetchMock instance for chaining
 */
fetch.mockResponses(...responses);

Usage Examples:

// Multiple string responses
fetch.mockResponses(
  "First response",
  "Second response", 
  "Third response"
);

// Mixed response types with configurations
fetch.mockResponses(
  [JSON.stringify({ page: 1 }), { status: 200 }],
  [JSON.stringify({ page: 2 }), { status: 200 }],
  ["Not found", { status: 404 }]
);

// Using functions
fetch.mockResponses(
  (req) => Promise.resolve(JSON.stringify({ url: req.url })),
  "Static response",
  [JSON.stringify({ final: true }), { status: 200 }]
);

Types

/**
 * Function type for generating dynamic responses
 */
type MockResponseInitFunction = (request: Request) => Promise<MockResponseInit | string>;

/**
 * Response configuration interface
 */
interface MockResponseInit extends MockParams {
  body?: string;
  init?: MockParams;
}

/**
 * Basic response parameters
 */
interface MockParams {
  status?: number;
  statusText?: string;
  headers?: string[][] | { [key: string]: string };
  url?: string;
}

docs

conditional-mocking.md

error-abort-handling.md

index.md

mock-management.md

response-mocking.md

tile.json