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

Commit e673b30

Browse files
Alex Nozdriukhindevelopit
Alex Nozdriukhin
authored andcommitted
Add the noop 'importScripts' worker function (#2)
* Add the noop 'importScripts' function * Add a test for 'importScripts'
1 parent 719e762 commit e673b30

File tree

4 files changed

+23
-1
lines changed

4 files changed

+23
-1
lines changed

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ global.Worker = function Worker(url) {
5757
postMessage(data) {
5858
outside.emit('message', { data });
5959
},
60-
fetch: global.fetch
60+
fetch: global.fetch,
61+
importScripts(...urls) {}
6162
},
6263
getScopeVar;
6364
inside.on('message', e => { let f = getScopeVar('onmessage'); if (f) f.call(scope, e); });

test/index.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import 'jsdom-worker';
22

3+
import fs from 'fs';
4+
import path from 'path';
5+
36
const sleep = t => new Promise( r => { setTimeout(r, t); });
47

58
describe('jsdom-worker', () => {
@@ -11,4 +14,14 @@ describe('jsdom-worker', () => {
1114
await sleep(10);
1215
expect(worker.onmessage).toHaveBeenCalledWith({ data: 10 });
1316
});
17+
18+
it('should work with importScripts', async () => {
19+
const mod = fs.readFileSync(path.join(__dirname, './module.js'));
20+
const code = fs.readFileSync(path.join(__dirname, './worker.js'));
21+
const worker = new Worker(URL.createObjectURL(new Blob([mod + code])));
22+
worker.onmessage = jest.fn();
23+
worker.postMessage();
24+
await sleep(10);
25+
expect(worker.onmessage).toHaveBeenCalledWith({ data: 'test' });
26+
});
1427
});

test/module.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable no-unused-vars */
2+
const importedModule = {
3+
string: 'test'
4+
};

test/worker.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* eslint-disable no-undef */
2+
importScripts('module.js');
3+
4+
onmessage = () => { postMessage(importedModule.string); };

0 commit comments

Comments
 (0)