/
error.spec.js
120 lines (100 loc) · 3.67 KB
/
error.spec.js
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
import { setGlobalConfig } from '../../index';
import { errorLoggerWithoutPromise } from '../error';
const printLog = jest.fn(() => {});
const axiosError = {
code: 500,
config: {
method: 'GET',
url: 'https://github.com/hg-pyun',
},
response: {
data: 'dummy data',
status: 500,
statusText: 'internal server error',
headers: '',
},
};
beforeEach(() => {
printLog.mockClear();
setGlobalConfig({ logger: printLog });
});
test('response should be return immutable axiosError', () => {
const mockLogger = jest.fn(errorLoggerWithoutPromise);
mockLogger(axiosError);
expect(mockLogger).toReturnWith(axiosError);
});
test('if erorr.config is undefined, return original error', () => {
const error = new Error('Unexpected error');
const mockLogger = jest.fn(errorLoggerWithoutPromise);
mockLogger(error);
expect(mockLogger).toReturnWith(error);
});
test('if config is undefined, logger make default log', () => {
const {
config: { url, method },
response: { data, status, statusText },
} = axiosError;
errorLoggerWithoutPromise(axiosError);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining('[Axios][Error]'));
expect(printLog).toBeCalledWith(expect.stringContaining(method));
expect(printLog).toBeCalledWith(expect.stringContaining(url));
expect(printLog).toBeCalledWith(expect.stringContaining(`${status}:${statusText}`));
expect(printLog).toBeCalledWith(expect.stringContaining(data));
});
test('if global config is defined only, logger make log with options', () => {
const globalConfig = {
prefixText: '[global custom prefix]',
};
setGlobalConfig(globalConfig);
errorLoggerWithoutPromise(axiosError);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining('[global custom prefix]'));
});
test('if local config is defined only, logger make log with options', () => {
const localConfig = {
prefixText: '[local custom prefix]',
};
errorLoggerWithoutPromise(axiosError, localConfig);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining('[local custom prefix]'));
});
test('if both global and local config are defined, local config should override global config', () => {
const globalConfig = {
prefixText: '[global custom prefix]',
};
const localConfig = {
prefixText: '[local custom prefix]',
};
setGlobalConfig(globalConfig);
errorLoggerWithoutPromise(axiosError, localConfig);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining('[local custom prefix]'));
});
test('if prefixText is false, remove prefix', () => {
const globalConfig = {
prefixText: false,
};
setGlobalConfig(globalConfig);
errorLoggerWithoutPromise(axiosError);
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.not.stringContaining('[Axios]'));
});
test('if custom logger is respected', () => {
const customPrintLog = jest.fn(() => {});
errorLoggerWithoutPromise(axiosError, { logger: customPrintLog });
expect(printLog).not.toHaveBeenCalled();
expect(customPrintLog).toHaveBeenCalled();
});
test('if baseUrl is taken into consideration', () => {
errorLoggerWithoutPromise({
...axiosError,
config: {
...axiosError.config,
baseURL: 'https://github.com/',
url: '/hg-pyun',
},
});
expect(printLog).toHaveBeenCalled();
expect(printLog).toBeCalledWith(expect.stringContaining(axiosError.config.url));
});