-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-error.test.ts
104 lines (96 loc) · 2.84 KB
/
fetch-error.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
import { FetchError } from './fetch-error.js';
describe('FetchError', () => {
test('minimum details', () => {
const error = new FetchError({
url: 'https://example.com',
status: 500,
});
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe(`Failed fetching https://example.com (500, unknown_error)`);
expect(error).toMatchObject({
name: 'FetchError',
method: undefined,
url: 'https://example.com',
status: 500,
headers: expect.objectContaining({}),
id: undefined,
code: 'unknown_error',
reason: undefined,
});
});
test('nulled details', () => {
const error = new FetchError({
method: null,
url: 'https://example.com',
status: 500,
headers: null,
id: null,
code: null,
reason: null,
});
expect(error).toMatchObject({
name: 'FetchError',
method: undefined,
url: 'https://example.com',
status: 500,
headers: expect.objectContaining({}),
id: undefined,
code: 'unknown_error',
reason: null,
});
});
test('all details', () => {
const reason = new Error('reason');
const error = new FetchError({
method: 'POST',
url: 'https://example.com',
status: 404,
headers: {
foo: 'bar',
...['allow', 'content-range', 'proxy-authenticate', 'retry-after', 'upgrade', 'www-authenticate'].reduce(
(result, key) => ({ ...result, [key]: key.toUpperCase() }),
{},
),
},
id: 'identifier',
code: 'known_error',
reason,
});
expect(error).toBeInstanceOf(Error);
expect(error.message).toBe(`Failed fetching POST https://example.com (404, known_error, identifier)`);
expect(error).toMatchObject({
name: 'FetchError',
method: 'POST',
url: 'https://example.com',
status: 404,
headers: expect.objectContaining({
...['allow', 'content-range', 'proxy-authenticate', 'retry-after', 'upgrade', 'www-authenticate'].reduce(
(result, key) => ({ ...result, [key]: key.toUpperCase() }),
{},
),
}),
id: 'identifier',
code: 'known_error',
reason,
});
});
test('headers is a Headers instance', () => {
const error = new FetchError({
url: 'https://example.com',
status: 500,
headers: new Headers({
foo: 'bar',
...['allow', 'content-range', 'proxy-authenticate', 'retry-after', 'upgrade', 'www-authenticate'].reduce(
(result, key) => ({ ...result, [key]: key.toUpperCase() }),
{},
),
}),
});
expect(error.headers).toEqual({
...['allow', 'content-range', 'proxy-authenticate', 'retry-after', 'upgrade', 'www-authenticate'].reduce(
(result, key) => ({ ...result, [key]: key.toUpperCase() }),
{},
),
});
});
});