-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
57 lines (42 loc) · 1.39 KB
/
index.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
const test = require('tape');
const { safeFunction, safePromise } = require('./index');
function iThrowThingsSometimes(val, shouldThrow) {
if (shouldThrow) {
throw new Error('Something went wrong');
} else {
return val;
}
}
function iRejectSometimes(val, shouldReject) {
return new Promise(function(resolve, reject) {
if (shouldReject) {
reject(new Error('Something went wrong'));
} else {
resolve(val);
}
});
}
test('safeFunction - should return result as part of a tuple', async function(t) {
t.plan(2);
const [error, result] = await safeFunction(iThrowThingsSometimes)(50);
t.equal(error, undefined);
t.equal(result, 50);
});
test('safeFunction - should return error as part of a tuple', async function(t) {
t.plan(2);
const [error, result] = await safeFunction(iThrowThingsSometimes)(50, true);
t.equal(error.message, 'Something went wrong');
t.equal(result, undefined);
});
test('safePromise - should return result as part of a tuple', async function(t) {
t.plan(2);
const [error, result] = await safePromise(iRejectSometimes(50));
t.equal(error, undefined);
t.equal(result, 50);
});
test('safePromise - should return error as part of a tuple', async function(t) {
t.plan(2);
const [error, result] = await safePromise(iRejectSometimes(50, true));
t.equal(error.message, 'Something went wrong');
t.equal(result, undefined);
});