-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.test.js
62 lines (57 loc) · 1.52 KB
/
errors.test.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
async function returnError() {
return new Promise((resolve, reject) =>
setTimeout(() => reject(new Error('newError')), 1000));
}
async function throwError() {
return new Promise((resolve, reject) => {
throw new Error('THROW');
// should never get here
console.log('paperino');
resolve('paperino');
})
}
test('try/catch should not catch new Error', async () => {
let result = '';
try {
result = await returnError();
// it will never get here
expect('should get here').toBe(false);
} catch (err) {
expect(err).toEqual(new Error('newError'));
}
expect.assertions(1);
});
test('.catch() should catch throw new Error', () => {
let result = '';
return returnError().then((res) => {
result = res;
// it will never get here
expect('should get here').toBe(false);
}).catch((err) => {
expect(err).toEqual(new Error('newError'));
expect.assertions(1);
})
});
test('try/catch should catch thrown new Error', async () => {
let result = '';
try {
result = await throwError();
// it will never get here
expect('should get here').toBe(false);
} catch (err) {
expect(err).toEqual(new Error('THROW'));
expect.assertions(1);
}
});
test('.catch() should catch thrown new Error', () => {
let result = '';
return throwError().then((res) => {
result = res;
// it will never get here
console.log(result);
expect('should get here').toBe(false);
}).catch((err) => {
expect(err).toEqual(new Error('THROW'));
expect.assertions(1);
})
});