MockHttpResponse and createMockHttpResponse
For advanced cases where you construct or inspect HttpResponse yourself.
Default status values
Statuses fall back to testingDefaults unless you set status explicitly.
MockHttpResponse
A test-oriented subclass of runtime HttpResponse. Constructor options include request info (requestParams, etc.), optional data / error / status, and optional httpClient for URL building.
- Use
setData/setErrorwhen you need to change payload or status after creation. createMockHttpResponse(params)is the async factory: it constructs aMockHttpResponseand awaitsresolveBody, sodata/errormatch ordinary client behavior before you use the instance.MockHttpResponse.fromEndpoint(endpoint, params)is the static factory that derivesrequestParams(andrequest.url) from a real endpoint — viaendpoint.configuration.params(input)and the endpoint'sHttpClient.data/error/statusare typed from the endpoint'sHttpResponse. Body resolution matches the constructor: callresolveBodyto applydata/error.
Example:
ts
import { createMockHttpResponse } from 'mobx-tanstack-query-api/testing';
const response = await createMockHttpResponse({
requestParams: { path: '/users', method: 'GET', format: 'json' },
data: { name: 'Ada' },
});
expect(response.data).toEqual({ name: 'Ada' });Example (fromEndpoint):
ts
import { MockHttpResponse } from 'mobx-tanstack-query-api/testing';
const response = MockHttpResponse.fromEndpoint(getUser, {
input: { id: 1 },
data: { name: 'Ada' },
});
expect(response.request.url).toBe('https://api.test/users/1');
await response.resolveBody('json');
expect(response.data).toEqual({ name: 'Ada' });