magnitude-test
Version:
A TypeScript client for running automated UI tests through the Magnitude testing platform
46 lines (45 loc) • 1.46 kB
JavaScript
import { parentPort, workerData } from "node:worker_threads";
import { isBun } from 'std-env';
import EventEmitter from "node:events";
if (!globalThis.__magnitudeTestFunctions) {
globalThis.__magnitudeTestFunctions = new Map();
}
export const testFunctions = globalThis.__magnitudeTestFunctions;
if (!globalThis.__magnitudeMessageEmitter) {
globalThis.__magnitudeMessageEmitter = new EventEmitter();
}
export const messageEmitter = globalThis.__magnitudeMessageEmitter;
if (!globalThis.__magnitudeTestHooks) {
globalThis.__magnitudeTestHooks = {
beforeAll: [],
afterAll: [],
beforeEach: [],
afterEach: [],
};
}
export const hooks = globalThis.__magnitudeTestHooks;
export function postToParent(message) {
if (isBun) {
if (typeof process.send !== 'function') {
throw new Error("Not running in a Bun subprocess with IPC");
}
process.send(message);
return;
}
if (!parentPort)
throw new Error("Not running in a worker thread");
parentPort.postMessage(message);
}
export function getTestWorkerData() {
if (isBun) {
const dataStr = process.env.MAGNITUDE_WORKER_DATA;
if (!dataStr) {
throw new Error('Worker data not found in environment');
}
return JSON.parse(dataStr);
}
if (!parentPort) {
throw new Error('Do not use this module on the main thread');
}
return workerData;
}