forked from developit/workerize-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
97 lines (83 loc) · 2.54 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
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
import './other';
import Worker from 'workerize-proxy-loader?ready&name=test!./worker';
import InlineWorker from 'workerize-proxy-loader?ready&inline&name=test!./worker';
describe('worker', () => {
let worker;
it('should be an instance of Worker', () => {
worker = new Worker();
expect(worker instanceof window.Worker).toBeTruthy();
});
it('worker.foo()', async () => {
expect(await worker.foo()).toBe(1);
});
it('worker.bar()', async () => {
let out = await worker.bar('a', 'b');
expect(out).toEqual('a [bar:3] b');
});
it('worker.throwError() should pass the Error back to the application context', async () => {
try {
await worker.throwError();
}
catch (e) {
expect(e).toEqual(Error('Error in worker.js'));
}
});
it('should fire ready event', done => {
let worker = new Worker(),
called = false,
isDone = false;
function fin() {
if (isDone) return;
isDone = true;
expect(called).toEqual(true);
done();
}
worker.addEventListener('ready', () => {
called = true;
fin();
});
setTimeout(fin, 300);
});
});
describe('async/await demo', () => {
it('remote worker', async () => {
let start = Date.now(), elapsed;
let worker = new Worker();
// passing "?ready" sets up this promise for you: (just a wrapper around the "ready" event)
await worker.ready;
elapsed = Date.now()-start;
console.log(`new Worker() [${elapsed}ms]`);
expect(elapsed).toBeLessThan(300);
let one = await worker.foo();
elapsed = Date.now()-start;
console.log(`await worker.foo() [${elapsed}ms]: `, one);
expect(one).toEqual(1);
expect(Date.now()-start).toBeLessThan(300); // @todo why the overhead here?
start = Date.now();
let two = await worker.bar(1, 2);
elapsed = Date.now()-start;
console.log(`await worker.bar(1, 2) [${elapsed}ms]: `, two);
expect(two).toEqual('1 [bar:3] 2');
expect(Date.now()-start).toBeLessThan(20);
});
it('inline worker', async () => {
let start = Date.now(), elapsed;
let worker = new InlineWorker();
await worker.ready;
elapsed = Date.now()-start;
console.log(`new InlineWorker() [${elapsed}ms]`);
expect(elapsed).toBeLessThan(300);
start = Date.now();
let one = await worker.foo();
elapsed = Date.now()-start;
console.log(`await worker.foo() [${elapsed}ms]: `, one);
expect(one).toEqual(1);
expect(elapsed).toBeLessThan(20);
start = Date.now();
let two = await worker.bar(1, 2);
elapsed = Date.now()-start;
console.log(`await worker.bar(1, 2) [${elapsed}ms]: `, two);
expect(two).toEqual('1 [bar:3] 2');
expect(elapsed).toBeLessThan(20);
});
});