-
Notifications
You must be signed in to change notification settings - Fork 10
/
getErrorMessage.test.ts
124 lines (105 loc) · 2.49 KB
/
getErrorMessage.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
/* eslint-disable no-throw-literal, jest/no-conditional-expect */
import { getErrorMessage } from './getErrorMessage';
test('throw new Error()', () => {
try {
throw new Error('message');
} catch (e) {
expect(getErrorMessage(e)).toEqual('message');
}
});
test('throw without new Error()', () => {
try {
throw 'message';
} catch (e) {
expect(getErrorMessage(e)).toEqual('"message"');
}
try {
throw 666;
} catch (e) {
expect(getErrorMessage(e)).toEqual('666');
}
try {
throw { error: 'message' };
} catch (e) {
expect(getErrorMessage(e)).toEqual('{"error":"message"}');
}
try {
throw [1, 2, 3];
} catch (e) {
expect(getErrorMessage(e)).toEqual('[1,2,3]');
}
try {
throw undefined;
} catch (e) {
expect(getErrorMessage(e)).toEqual('');
}
try {
// eslint-disable-next-line unicorn/no-null
throw null;
} catch (e) {
expect(getErrorMessage(e)).toEqual('null');
}
});
test('circular reference', () => {
{
const circular = {
error: 'message'
};
// @ts-ignore
circular.myself = circular;
try {
JSON.stringify(circular);
} catch (e) {
expect(getErrorMessage(e)).toEqual(
'Converting circular structure to JSON\n' +
" --> starting at object with constructor 'Object'\n" +
" --- property 'myself' closes the circle"
);
}
try {
throw circular;
} catch (e) {
expect(getErrorMessage(e)).toEqual('[object Object]');
}
}
{
const circular = [1, 2, 3];
// @ts-ignore
circular[0] = circular;
try {
JSON.stringify(circular);
} catch (e) {
expect(getErrorMessage(e)).toEqual(
'Converting circular structure to JSON\n' +
" --> starting at object with constructor 'Array'\n" +
' --- index 0 closes the circle'
);
}
try {
throw circular;
} catch (e) {
expect(getErrorMessage(e)).toEqual(',2,3');
}
}
{
class Circular {
error = 'message';
myself = this;
}
const circular = new Circular();
try {
JSON.stringify(circular);
} catch (e) {
expect(getErrorMessage(e)).toEqual(
'Converting circular structure to JSON\n' +
" --> starting at object with constructor 'Circular'\n" +
" --- property 'myself' closes the circle"
);
}
try {
throw circular;
} catch (e) {
expect(getErrorMessage(e)).toEqual('[object Object]');
}
}
});