Stratal API Reference
    Preparing search index...

    Wrapper around Cloudflare's fetchMock for declarative fetch mocking in tests

    Based on undici's MockAgent, fetchMock.get(origin) returns a MockPool for that origin. The MockPool's intercept() method is used to define which requests to mock.

    import { createFetchMock } from '@stratal/testing'

    const mock = createFetchMock()

    beforeEach(() => {
    mock.activate()
    mock.disableNetConnect()
    })

    afterEach(() => {
    mock.reset()
    })

    it('should mock external API', async () => {
    // Using helper method
    mock.mockJsonResponse('https://api.example.com/data', { success: true })

    // Or using direct API
    mock.get('https://api.example.com')
    .intercept({ path: '/users', method: 'POST' })
    .reply(201, JSON.stringify({ created: true }))

    const response = await fetch('https://api.example.com/data')
    const json = await response.json()

    expect(json.success).toBe(true)
    mock.assertNoPendingInterceptors()
    })
    Index

    Constructors

    Methods

    • Assert that all defined interceptors were called

      Returns void

      If there are pending interceptors that weren't matched

      it('should call all mocked endpoints', async () => {
      mock.mockJsonResponse('https://api.example.com/data', { data: [] })

      await fetch('https://api.example.com/data')

      mock.assertNoPendingInterceptors() // Pass
      })
    • Disable all network connections, forcing all requests to use mocks

      Returns void

      beforeEach(() => {
      mock.activate()
      mock.disableNetConnect() // Ensure all requests are mocked
      })
    • Enable network connections for specific hosts

      Parameters

      • Optionalhost: string | RegExp

      Returns void

      mock.enableNetConnect('localhost')
      mock.enableNetConnect(/^https://trusted\.com/)
    • Get a MockPool for the specified origin

      This is the underlying fetchMock.get() method that returns a MockPool for mocking requests to the specified origin.

      Parameters

      Returns Interceptable

      MockPool for chaining .intercept() and .reply()

      // Mock a GET request
      mock.get('https://api.example.com')
      .intercept({ path: '/users', method: 'GET' })
      .reply(200, JSON.stringify({ users: [] }))

      // Mock a POST request with body matching
      mock.get('https://api.example.com')
      .intercept({
      path: '/users',
      method: 'POST',
      body: (body) => body.includes('test')
      })
      .reply(201, JSON.stringify({ created: true }))
    • Helper method to mock error responses

      Parameters

      • url: string

        Full URL to mock

      • status: number

        HTTP error status code

      • Optionalmessage: string

        Optional error message

      • options: MockErrorOptions = {}

        Additional options for the error mock

      Returns MockScope<object>

      // Mock a 401 error
      mock.mockError('https://api.example.com/fail', 401, 'Unauthorized')

      // Mock a 500 error with custom method
      mock.mockError(
      'https://api.example.com/fail',
      500,
      'Server Error',
      { method: 'POST' }
      )
    • Helper method to mock JSON responses

      Automatically parses the URL into origin and path, and sets up the mock.

      Parameters

      Returns MockScope<object>

      // Mock a GET request
      mock.mockJsonResponse('https://api.example.com/users', { users: [] })

      // Mock a POST request
      mock.mockJsonResponse(
      'https://api.example.com/users',
      { created: true },
      { method: 'POST', status: 201 }
      )

      // With custom headers and delay
      mock.mockJsonResponse(
      'https://api.example.com/users',
      { users: [] },
      {
      status: 200,
      method: 'GET',
      headers: { 'X-Custom': 'value' },
      delay: 100
      }
      )
    • Generic helper to mock any HTTP request

      Parameters

      • origin: string

        The origin URL (e.g., 'https://api.example.com')

      • options: { body?: unknown; method?: string; path: string; status?: number }

        Request matching and response options

      Returns MockScope<object>

      mock.mockRequest('https://api.example.com', {
      path: '/users',
      method: 'PUT',
      status: 200,
      body: { updated: true }
      })