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 */
|
||||
"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) => {
|
||||
|
@ -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 engine = example.engine;
|
||||
|
@ -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,
|
||||
|
|
|
@ -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';
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
@ -318,6 +325,6 @@ const comparisonReport = (capturesDev, capturesBuild, devSize, buildSize, buildV
|
|||
};
|
||||
|
||||
module.exports = {
|
||||
engineCapture, comparisonReport, logReport,
|
||||
requireUncached, engineCapture, comparisonReport, logReport,
|
||||
toMatchExtrinsics, toMatchIntrinsics
|
||||
};
|
Loading…
Reference in a new issue