index.spec.js
2.28 KB
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
import util from 'util';
import MockAdapter from 'axios-mock-adapter';
import axios from 'axios';
import AxiosError from '..';
const mock = new MockAdapter(axios);
mock.onAny().reply(400, {
error_status: 'boom....',
});
const stack = `Error: boom....
at Object.<anonymous> (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:16:19)
at Generator.throw (<anonymous>)
at step (/Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:336)
at /Users/xxx/messaging-apis/packages/axios-error/src/__tests__/index.spec.js:4:535
at <anonymous>
`;
it('should work', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite because axios-mock-adapter set it to undefined
err.response.statusText = 'Bad Request';
const error = new AxiosError(err.response.data.error_status, err);
// overwrite stack to test it
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
}
});
it('should set `.status` property', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite because axios-mock-adapter set it to undefined
err.response.statusText = 'Bad Request';
const error = new AxiosError(err);
expect(error.status).toBe(400);
}
});
it('should work with construct using error instance only', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite because axios-mock-adapter set it to undefined
err.response.statusText = 'Bad Request';
const error = new AxiosError(err);
// overwrite stack to test it
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
}
});
it('should work with undefined response', async () => {
try {
await axios.post('/', { x: 1 });
} catch (err) {
// overwrite to undefined
// https://github.com/Yoctol/bottender/issues/246
err.response = undefined;
const error = new AxiosError('read ECONNRESET', err);
// overwrite stack to test it
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
}
});
it('should support error without axios data', () => {
const error = new AxiosError('custom error');
error.stack = stack;
expect(error[util.inspect.custom]()).toMatchSnapshot();
});