Skip to content

Commit

Permalink
chore(test): add web worker polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
Desplandis committed Jun 24, 2024
1 parent c3a67a3 commit cec42ad
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/unit/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,43 @@
import fetch from 'node-fetch';
import { Camera } from 'three';
import { DOMParser } from '@xmldom/xmldom';
import threads from 'worker_threads';

const WORKER = Symbol('worker');

class Worker extends EventTarget {
constructor(url) {
super();

const worker = new threads.Worker(url);
this[WORKER] = worker;

worker.on('message', (data) => {
const event = new Event('message');
event.data = data;
this.dispatchEvent(event);
});

worker.on('error', () => {
const event = new Event('error');
this.dispatchEvent(event);
});

worker.on('messageerror', (data) => {
const event = new Event('messageError');
event.data = data;
this.dispatchEvent(event);
});
}

postMessage(data, transferList) {
this[WORKER].postMessage(data, transferList);
}

terminate() {
this[WORKER].terminate();
}
}

global.window = {
addEventListener: () => {},
Expand All @@ -10,6 +47,8 @@ global.window = {
setTimeout,
};

global.Worker = Worker;

global.requestAnimationFrame = () => {};
global.fetch = fetch;
global.fetch.Promise = Promise;
Expand Down

0 comments on commit cec42ad

Please sign in to comment.