From ca2fe752d33fce63cf9d9502d3828d8a983a7d76 Mon Sep 17 00:00:00 2001 From: liabru Date: Sun, 28 Nov 2021 21:25:11 +0000 Subject: [PATCH] refactor test worker and prevent test cache --- test/ExampleWorker.js | 66 +++++++++++++++++++++++-------------------- test/Examples.spec.js | 15 +++++----- test/TestTools.js | 9 +++++- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/test/ExampleWorker.js b/test/ExampleWorker.js index 791c808..2b058f8 100644 --- a/test/ExampleWorker.js +++ b/test/ExampleWorker.js @@ -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, diff --git a/test/Examples.spec.js b/test/Examples.spec.js index efb2dc9..00a85dc 100644 --- a/test/Examples.spec.js +++ b/test/Examples.spec.js @@ -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. diff --git a/test/TestTools.js b/test/TestTools.js index 8504adf..525e547 100644 --- a/test/TestTools.js +++ b/test/TestTools.js @@ -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 }; \ No newline at end of file