Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 4a400f7

Browse files
Alex Nozdriukhindevelopit
Alex Nozdriukhin
authored andcommitted
Add a semicolon for IIFE workers to work (developit#3)
* Add a semicolon IIFE's won't work without it * Add a test for IIFE
1 parent e673b30 commit 4a400f7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ global.Worker = function Worker(url) {
7878
.then( code => {
7979
let vars = 'var self=this,global=self';
8080
for (let k in scope) vars += `,${k}=self.${k}`;
81-
getScopeVar = eval('(function() {'+vars+'\n'+code+'\nreturn function(__){return eval(__)}})').call(scope);
81+
getScopeVar = eval('(function() {'+vars+';\n'+code+'\nreturn function(__){return eval(__)}})').call(scope);
8282
let q = messageQueue;
8383
messageQueue = null;
8484
q.forEach(this.postMessage);

test/index.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,14 @@ describe('jsdom-worker', () => {
2424
await sleep(10);
2525
expect(worker.onmessage).toHaveBeenCalledWith({ data: 'test' });
2626
});
27+
28+
it('should work with IIFE', async () => {
29+
const n = Math.random();
30+
const code = `(function(n){ onmessage = e => { postMessage(n) } })(${n})`;
31+
const worker = new Worker(URL.createObjectURL(new Blob([code])));
32+
worker.onmessage = jest.fn();
33+
worker.postMessage();
34+
await sleep(10);
35+
expect(worker.onmessage).toHaveBeenCalledWith({ data: n });
36+
});
2737
});

0 commit comments

Comments
 (0)