Assert that all defined interceptors were called
Deactivate fetch mocking
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.
The origin URL (e.g., 'https://api.example.com')
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
Full URL to mock
HTTP error status code
Optionalmessage: stringOptional error message
Additional options for the error mock
Helper method to mock JSON responses
Automatically parses the URL into origin and path, and sets up the mock.
Full URL to mock (e.g., 'https://api.example.com/users')
JSON data to return
Additional options for the mock
// 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
The origin URL (e.g., 'https://api.example.com')
Request matching and response options
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.
Example