0
# Response Mocking
1
2
Core functionality for mocking successful HTTP responses with configurable bodies and options. Supports both one-time and persistent mocks with flexible response generation.
3
4
## Capabilities
5
6
### Mock Response
7
8
Sets up a mock response for all subsequent fetch calls until reset or changed.
9
10
```javascript { .api }
11
/**
12
* Mock response for all subsequent fetch calls
13
* @param bodyOrFunction - Response body string or function that generates response
14
* @param init - Optional response configuration (status, headers, etc.)
15
* @returns FetchMock instance for chaining
16
*/
17
fetch.mockResponse(bodyOrFunction, init);
18
```
19
20
**Usage Examples:**
21
22
```javascript
23
// Simple string response
24
fetch.mockResponse("Hello World");
25
26
// JSON response with status
27
fetch.mockResponse(JSON.stringify({ data: "test" }), {
28
status: 200,
29
headers: { "Content-Type": "application/json" }
30
});
31
32
// Dynamic response function
33
fetch.mockResponse((request) => {
34
if (request.url.includes("/api/users")) {
35
return Promise.resolve(JSON.stringify({ users: [] }));
36
}
37
return Promise.resolve("Not found");
38
});
39
```
40
41
### Mock Response Once
42
43
Sets up a mock response for the next fetch call only. Subsequent calls will use the default behavior or other configured mocks.
44
45
```javascript { .api }
46
/**
47
* Mock response for the next fetch call only
48
* @param bodyOrFunction - Response body string or function that generates response
49
* @param init - Optional response configuration (status, headers, etc.)
50
* @returns FetchMock instance for chaining
51
*/
52
fetch.mockResponseOnce(bodyOrFunction, init);
53
```
54
55
**Usage Examples:**
56
57
```javascript
58
// Mock single response
59
fetch.mockResponseOnce(JSON.stringify({ id: 1, name: "Alice" }));
60
61
// Chain multiple one-time responses
62
fetch
63
.mockResponseOnce(JSON.stringify({ page: 1 }))
64
.mockResponseOnce(JSON.stringify({ page: 2 }));
65
66
// With custom status and headers
67
fetch.mockResponseOnce("Server Error", {
68
status: 500,
69
statusText: "Internal Server Error",
70
headers: { "Content-Type": "text/plain" }
71
});
72
```
73
74
### Once (Alias)
75
76
Convenient alias for `mockResponseOnce` providing the same functionality with shorter syntax.
77
78
```javascript { .api }
79
/**
80
* Alias for mockResponseOnce - mock response for the next fetch call only
81
* @param bodyOrFunction - Response body string or function that generates response
82
* @param init - Optional response configuration (status, headers, etc.)
83
* @returns FetchMock instance for chaining
84
*/
85
fetch.once(bodyOrFunction, init);
86
```
87
88
**Usage Examples:**
89
90
```javascript
91
// Same as mockResponseOnce
92
fetch.once(JSON.stringify({ success: true }));
93
94
// Chaining with alias
95
fetch
96
.once("First response")
97
.once("Second response")
98
.once("Third response");
99
```
100
101
### Mock Responses
102
103
Sets up multiple mock responses that will be used in sequence. Each response is used once, in the order provided.
104
105
```javascript { .api }
106
/**
107
* Mock multiple responses in sequence
108
* @param responses - Array of responses (strings, arrays with [body, init], or functions)
109
* @returns FetchMock instance for chaining
110
*/
111
fetch.mockResponses(...responses);
112
```
113
114
**Usage Examples:**
115
116
```javascript
117
// Multiple string responses
118
fetch.mockResponses(
119
"First response",
120
"Second response",
121
"Third response"
122
);
123
124
// Mixed response types with configurations
125
fetch.mockResponses(
126
[JSON.stringify({ page: 1 }), { status: 200 }],
127
[JSON.stringify({ page: 2 }), { status: 200 }],
128
["Not found", { status: 404 }]
129
);
130
131
// Using functions
132
fetch.mockResponses(
133
(req) => Promise.resolve(JSON.stringify({ url: req.url })),
134
"Static response",
135
[JSON.stringify({ final: true }), { status: 200 }]
136
);
137
```
138
139
## Types
140
141
```typescript { .api }
142
/**
143
* Function type for generating dynamic responses
144
*/
145
type MockResponseInitFunction = (request: Request) => Promise<MockResponseInit | string>;
146
147
/**
148
* Response configuration interface
149
*/
150
interface MockResponseInit extends MockParams {
151
body?: string;
152
init?: MockParams;
153
}
154
155
/**
156
* Basic response parameters
157
*/
158
interface MockParams {
159
status?: number;
160
statusText?: string;
161
headers?: string[][] | { [key: string]: string };
162
url?: string;
163
}
164
```