Skip to content

Commit

Permalink
refactor test worker and prevent test cache
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Nov 28, 2021
1 parent caeb07e commit ca2fe75
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
66 changes: 36 additions & 30 deletions test/ExampleWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,55 @@
/* eslint no-global-assign: 0 */
"use strict";

const stubBrowserFeatures = M => {
const noop = () => ({ collisionFilter: {}, mouse: {} });
M.Render.create = () => ({ options: {}, bounds: { min: { x: 0, y: 0 }, max: { x: 800, y: 600 }}});
M.Render.run = M.Render.lookAt = noop;
M.Runner.create = M.Runner.run = noop;
M.MouseConstraint.create = M.Mouse.create = noop;
M.Common.info = M.Common.warn = M.Common.log;
return M;
};

const reset = M => {
M.Common._nextId = M.Common._seed = 0;
M.Body._nextCollidingGroupId = 1;
M.Body._nextNonCollidingGroupId = -1;
M.Body._nextCategory = 0x0001;
};

const mock = require('mock-require');
const { engineCapture } = require('./TestTools');
const MatterDev = stubBrowserFeatures(require('../build/matter.dev'));
const MatterBuild = stubBrowserFeatures(require('../build/matter'));
const { requireUncached, engineCapture } = require('./TestTools');
const Example = require('../examples/index');
const consoleOriginal = global.console;

const runExample = options => {
const Matter = options.useDev ? MatterDev : MatterBuild;
const consoleOriginal = global.console;
const logs = [];
const prepareMatter = (options) => {
const Matter = requireUncached(options.useDev ? '../build/matter.dev' : '../build/matter');

if (Matter.Common._nextId !== 0) {
throw 'Matter instance has already been used.';
}

const noop = () => ({ collisionFilter: {}, mouse: {} });

Matter.Render.create = () => ({ options: {}, bounds: { min: { x: 0, y: 0 }, max: { x: 800, y: 600 }}});
Matter.Render.run = Matter.Render.lookAt = noop;
Matter.Runner.create = Matter.Runner.run = noop;
Matter.MouseConstraint.create = Matter.Mouse.create = noop;
Matter.Common.info = Matter.Common.warn = Matter.Common.log;

return Matter;
};

const prepareEnvironment = Matter => {
mock('matter-js', Matter);
global.Matter = Matter;

const logs = [];
global.document = global.window = { addEventListener: () => {} };
global.console = {
log: (...args) => {
logs.push(args.join(' '));
}
};

reset(Matter);
return logs;
};

const resetEnvironment = () => {
global.console = consoleOriginal;
global.window = undefined;
global.document = undefined;
global.Matter = undefined;
mock.stopAll();
};

const runExample = options => {
const Matter = prepareMatter(options);
const logs = prepareEnvironment(Matter);

const example = Example[options.name]();
const engine = example.engine;
Expand Down Expand Up @@ -89,11 +99,7 @@ const runExample = options => {
}
}

global.console = consoleOriginal;
global.window = undefined;
global.document = undefined;
global.Matter = undefined;
mock.stopAll();
resetEnvironment();

return {
name: options.name,
Expand Down
15 changes: 8 additions & 7 deletions test/Examples.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ jest.setTimeout(30 * 1000);

const fs = require('fs');

const {
const {
requireUncached,
comparisonReport,
logReport,
toMatchExtrinsics,
toMatchIntrinsics
} = require('./TestTools');

const Example = require('../examples/index');
const MatterBuild = require('../build/matter');
const { versionSatisfies } = require('../src/core/Plugin');
const Example = requireUncached('../examples/index');
const MatterBuild = requireUncached('../build/matter');
const { versionSatisfies } = requireUncached('../src/core/Plugin');
const Worker = require('jest-worker').default;

const testComparison = process.env.COMPARE === 'true';
Expand All @@ -30,7 +31,7 @@ const examples = Object.keys(Example).filter(key => {
return !excluded && supported;
});

const runExamples = async useDev => {
const captureExamples = async useDev => {
const worker = new Worker(require.resolve('./ExampleWorker'), {
enableWorkerThreads: true,
numWorkers: 1
Expand All @@ -48,8 +49,8 @@ const runExamples = async useDev => {
return result.reduce((out, capture) => (out[capture.name] = capture, out), {});
};

const capturesDev = runExamples(true);
const capturesBuild = runExamples(false);
const capturesDev = captureExamples(true);
const capturesBuild = captureExamples(false);

afterAll(async () => {
// Report experimental capture comparison.
Expand Down
9 changes: 8 additions & 1 deletion test/TestTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ const limit = (val, precision=3) => parseFloat(val.toPrecision(precision));
const toPercent = val => (100 * val).toPrecision(3);
const toPercentRound = val => Math.round(100 * val);

const requireUncached = path => {
delete require.cache[require.resolve(path)];
const module = require(path);
delete require.cache[require.resolve(path)];
return module;
};

const noiseThreshold = (val, threshold) => {
const sign = val < 0 ? -1 : 1;
const magnitude = Math.abs(val);
Expand Down Expand Up @@ -318,6 +325,6 @@ const comparisonReport = (capturesDev, capturesBuild, devSize, buildSize, buildV
};

module.exports = {
engineCapture, comparisonReport, logReport,
requireUncached, engineCapture, comparisonReport, logReport,
toMatchExtrinsics, toMatchIntrinsics
};

0 comments on commit ca2fe75

Please sign in to comment.