-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest-axios-snapshot.test.ts
212 lines (197 loc) · 4.35 KB
/
jest-axios-snapshot.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { defaultTransformer, setResponseTransformer } from './jest-axios-snapshot.js';
afterEach(() => {
setResponseTransformer(defaultTransformer);
});
it('should serialize response bodies', () => {
const response = {
config: {},
status: 200,
statusText: 'OK',
headers: { connection: 'close', 'content-type': 'application/json' },
data: {
arrays: [],
objects: {},
true: true,
false: false,
null: null,
strings: 'Hello',
nested: {
object: {
containing: {
numbers: 42,
arrays: [],
objects: {},
true: true,
false: false,
null: null,
strings: 'Hello',
},
},
arrays: ['containing', 1337, [true, false, []]],
},
},
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 200 OK
Connection: close
Content-Type: application/json
{
"arrays": [],
"false": false,
"nested": {
"arrays": [
"containing",
1337,
[
true,
false,
[],
],
],
"object": {
"containing": {
"arrays": [],
"false": false,
"null": null,
"numbers": 42,
"objects": {},
"strings": "Hello",
"true": true,
},
},
},
"null": null,
"objects": {},
"strings": "Hello",
"true": true,
}
`);
});
it('should support empty responses', () => {
const response = {
config: {},
status: 200,
statusText: 'No Content',
headers: { connection: 'close' },
data: '',
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 200 No Content
Connection: close
`);
});
it('should support a custom status text', () => {
const response = {
config: {},
status: 404,
statusText: 'Not here',
headers: { connection: 'close' },
data: '',
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 404 Not here
Connection: close
`);
});
it('should support a empty status text', () => {
const response = {
config: {},
status: 404,
statusText: '',
headers: { connection: 'close' },
data: '',
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 404 Not Found
Connection: close
`);
});
it('should support unknown status codes', () => {
const response = {
config: {},
status: 222,
statusText: '',
headers: { connection: 'close' },
data: '',
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 222
Connection: close
`);
});
it('should serialize text response bodies as-is', () => {
const response = {
config: {},
status: 200,
statusText: 'OK',
headers: { connection: 'close', 'content-type': 'text/html' },
data: '<!doctype html>\n<html>\n <body>\n <h1>Test</h1>\n </body>\n</html>',
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 200 OK
Connection: close
Content-Type: text/html
<!doctype html>
<html>
<body>
<h1>Test</h1>
</body>
</html>
`);
});
it('should support a custom response transformer', () => {
setResponseTransformer((config) => ({
...config,
status: 1337,
}));
const response = {
config: {},
status: 200,
statusText: 'OK',
headers: { connection: 'close', 'content-type': 'application/json' },
data: {},
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 1337 OK
Connection: close
Content-Type: application/json
{}
`);
});
it('should header printers', () => {
setResponseTransformer((config) => ({
...config,
status: 1337,
}));
const response = {
config: {},
status: 200,
statusText: 'OK',
headers: { connection: 'close', 'content-type': expect.any(String) },
data: {},
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 1337 OK
Connection: close
Content-Type: Any<String>
{}
`);
});
it('should not crash on text content if no content-type is defined', () => {
setResponseTransformer((config) => ({
...config,
status: 1337,
}));
const response = {
config: {},
status: 200,
statusText: 'OK',
headers: { connection: 'close' },
data: 'text',
};
expect(response).toMatchInlineSnapshot(`
HTTP/1.1 1337 OK
Connection: close
"text"
`);
});