mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-27 09:50:52 -05:00
refactor test worker and prevent test cache
This commit is contained in:
parent
caeb07e2d2
commit
ca2fe752d3
3 changed files with 52 additions and 38 deletions
|
@ -2,37 +2,34 @@
|
||||||
/* eslint no-global-assign: 0 */
|
/* eslint no-global-assign: 0 */
|
||||||
"use strict";
|
"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 mock = require('mock-require');
|
||||||
const { engineCapture } = require('./TestTools');
|
const { requireUncached, engineCapture } = require('./TestTools');
|
||||||
const MatterDev = stubBrowserFeatures(require('../build/matter.dev'));
|
|
||||||
const MatterBuild = stubBrowserFeatures(require('../build/matter'));
|
|
||||||
const Example = require('../examples/index');
|
const Example = require('../examples/index');
|
||||||
|
|
||||||
const runExample = options => {
|
|
||||||
const Matter = options.useDev ? MatterDev : MatterBuild;
|
|
||||||
const consoleOriginal = global.console;
|
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);
|
mock('matter-js', Matter);
|
||||||
global.Matter = Matter;
|
global.Matter = Matter;
|
||||||
|
|
||||||
|
const logs = [];
|
||||||
global.document = global.window = { addEventListener: () => {} };
|
global.document = global.window = { addEventListener: () => {} };
|
||||||
global.console = {
|
global.console = {
|
||||||
log: (...args) => {
|
log: (...args) => {
|
||||||
|
@ -40,7 +37,20 @@ const runExample = options => {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
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 example = Example[options.name]();
|
||||||
const engine = example.engine;
|
const engine = example.engine;
|
||||||
|
@ -89,11 +99,7 @@ const runExample = options => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
global.console = consoleOriginal;
|
resetEnvironment();
|
||||||
global.window = undefined;
|
|
||||||
global.document = undefined;
|
|
||||||
global.Matter = undefined;
|
|
||||||
mock.stopAll();
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: options.name,
|
name: options.name,
|
||||||
|
|
|
@ -6,15 +6,16 @@ jest.setTimeout(30 * 1000);
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
requireUncached,
|
||||||
comparisonReport,
|
comparisonReport,
|
||||||
logReport,
|
logReport,
|
||||||
toMatchExtrinsics,
|
toMatchExtrinsics,
|
||||||
toMatchIntrinsics
|
toMatchIntrinsics
|
||||||
} = require('./TestTools');
|
} = require('./TestTools');
|
||||||
|
|
||||||
const Example = require('../examples/index');
|
const Example = requireUncached('../examples/index');
|
||||||
const MatterBuild = require('../build/matter');
|
const MatterBuild = requireUncached('../build/matter');
|
||||||
const { versionSatisfies } = require('../src/core/Plugin');
|
const { versionSatisfies } = requireUncached('../src/core/Plugin');
|
||||||
const Worker = require('jest-worker').default;
|
const Worker = require('jest-worker').default;
|
||||||
|
|
||||||
const testComparison = process.env.COMPARE === 'true';
|
const testComparison = process.env.COMPARE === 'true';
|
||||||
|
@ -30,7 +31,7 @@ const examples = Object.keys(Example).filter(key => {
|
||||||
return !excluded && supported;
|
return !excluded && supported;
|
||||||
});
|
});
|
||||||
|
|
||||||
const runExamples = async useDev => {
|
const captureExamples = async useDev => {
|
||||||
const worker = new Worker(require.resolve('./ExampleWorker'), {
|
const worker = new Worker(require.resolve('./ExampleWorker'), {
|
||||||
enableWorkerThreads: true,
|
enableWorkerThreads: true,
|
||||||
numWorkers: 1
|
numWorkers: 1
|
||||||
|
@ -48,8 +49,8 @@ const runExamples = async useDev => {
|
||||||
return result.reduce((out, capture) => (out[capture.name] = capture, out), {});
|
return result.reduce((out, capture) => (out[capture.name] = capture, out), {});
|
||||||
};
|
};
|
||||||
|
|
||||||
const capturesDev = runExamples(true);
|
const capturesDev = captureExamples(true);
|
||||||
const capturesBuild = runExamples(false);
|
const capturesBuild = captureExamples(false);
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
// Report experimental capture comparison.
|
// Report experimental capture comparison.
|
||||||
|
|
|
@ -34,6 +34,13 @@ const limit = (val, precision=3) => parseFloat(val.toPrecision(precision));
|
||||||
const toPercent = val => (100 * val).toPrecision(3);
|
const toPercent = val => (100 * val).toPrecision(3);
|
||||||
const toPercentRound = val => Math.round(100 * val);
|
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 noiseThreshold = (val, threshold) => {
|
||||||
const sign = val < 0 ? -1 : 1;
|
const sign = val < 0 ? -1 : 1;
|
||||||
const magnitude = Math.abs(val);
|
const magnitude = Math.abs(val);
|
||||||
|
@ -318,6 +325,6 @@ const comparisonReport = (capturesDev, capturesBuild, devSize, buildSize, buildV
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
engineCapture, comparisonReport, logReport,
|
requireUncached, engineCapture, comparisonReport, logReport,
|
||||||
toMatchExtrinsics, toMatchIntrinsics
|
toMatchExtrinsics, toMatchIntrinsics
|
||||||
};
|
};
|
Loading…
Reference in a new issue