-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
107 lines (90 loc) · 2.53 KB
/
Copy pathtest.js
File metadata and controls
107 lines (90 loc) · 2.53 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import test from 'ava';
import offloadFunction from './index.js';
test('executes a basic function', async t => {
const result = await offloadFunction(() => 42);
t.is(result, 42);
});
test('passes arguments to function', async t => {
const result = await offloadFunction((a, b) => a + b, 2, 3);
t.is(result, 5);
});
test('returns a string', async t => {
const result = await offloadFunction(() => 'hello');
t.is(result, 'hello');
});
test('returns an object', async t => {
const result = await offloadFunction(() => ({name: 'test', value: 42}));
t.deepEqual(result, {name: 'test', value: 42});
});
test('returns an array', async t => {
const result = await offloadFunction(() => [1, 2, 3]);
t.deepEqual(result, [1, 2, 3]);
});
test('returns null', async t => {
const result = await offloadFunction(() => null);
t.is(result, null);
});
test('returns undefined', async t => {
const result = await offloadFunction(() => undefined);
t.is(result, undefined);
});
test('handles async function', async t => {
const result = await offloadFunction(async x => x * 2, 5);
t.is(result, 10);
});
test('propagates errors', async t => {
await t.throwsAsync(
() => offloadFunction(() => {
throw new Error('test error');
}),
{message: 'test error'},
);
});
test('propagates error name', async t => {
const error = await t.throwsAsync(() => offloadFunction(() => {
const error = new TypeError('type mismatch');
throw error;
}));
t.is(error.name, 'TypeError');
});
test('handles CPU-intensive work', async t => {
const result = await offloadFunction(n => {
let sum = 0;
for (let i = 0; i < n; i++) {
sum += i;
}
return sum;
}, 1000);
t.is(result, 499_500);
});
test('multiple arguments of different types', async t => {
const result = await offloadFunction(
(string_, number_, boolean_) => ({string_, number_, boolean_}),
'hello',
42,
true,
);
t.deepEqual(result, {string_: 'hello', number_: 42, boolean_: true});
});
test('returns a promise', t => {
const result = offloadFunction(() => 1);
t.true(result instanceof Promise);
});
test('handles function with no return value', async t => {
const result = await offloadFunction(() => {});
t.is(result, undefined);
});
test('handles nested object arguments', async t => {
const result = await offloadFunction(
object => object.nested.value,
{nested: {value: 'deep'}},
);
t.is(result, 'deep');
});
test('handles array arguments', async t => {
const result = await offloadFunction(
array => array.reduce((a, b) => a + b, 0),
[1, 2, 3, 4, 5],
);
t.is(result, 15);
});