0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00
liabru-matter-js/test/Examples.spec.js

100 lines
3.2 KiB
JavaScript
Raw Normal View History

2019-09-28 10:15:54 -04:00
/* eslint-env es6 */
2019-09-14 14:31:31 -04:00
"use strict";
jest.setTimeout(30 * 1000);
2019-09-14 14:31:31 -04:00
const fs = require('fs');
const {
requireUncached,
2021-01-28 19:09:27 -05:00
comparisonReport,
logReport,
toMatchExtrinsics,
toMatchIntrinsics
} = require('./TestTools');
2019-09-14 14:31:31 -04:00
const Example = requireUncached('../examples/index');
const MatterBuild = requireUncached('../build/matter');
const { versionSatisfies } = requireUncached('../src/core/Plugin');
const Worker = require('jest-worker').default;
2019-09-14 14:31:31 -04:00
const testComparison = process.env.COMPARE === 'true';
const saveComparison = process.env.SAVE === 'true';
2020-03-09 16:56:06 -04:00
const excludeExamples = ['svg', 'terrain'];
const excludeJitter = ['stack', 'circleStack', 'restitution', 'staticFriction', 'friction', 'newtonsCradle', 'catapult'];
const examples = Object.keys(Example).filter(key => {
const excluded = excludeExamples.includes(key);
const buildVersion = MatterBuild.version;
const exampleFor = Example[key].for;
const supported = versionSatisfies(buildVersion, exampleFor);
return !excluded && supported;
});
2019-09-14 14:31:31 -04:00
const captureExamples = async useDev => {
const worker = new Worker(require.resolve('./ExampleWorker'), {
enableWorkerThreads: true,
numWorkers: 1
});
2019-09-30 18:28:17 -04:00
const result = await Promise.all(examples.map(name => worker.runExample({
name,
useDev,
2020-03-09 16:56:06 -04:00
totalUpdates: 120,
jitter: excludeJitter.includes(name) ? 0 : 1e-10
})));
2019-09-30 18:28:17 -04:00
await worker.end();
2019-09-30 18:28:17 -04:00
return result.reduce((out, capture) => (out[capture.name] = capture, out), {});
2019-09-30 18:28:17 -04:00
};
const capturesDev = captureExamples(true);
const capturesBuild = captureExamples(false);
afterAll(async () => {
// Report experimental capture comparison.
const dev = await capturesDev;
const build = await capturesBuild;
2021-01-28 19:09:27 -05:00
const buildSize = fs.statSync('./build/matter.min.js').size;
const devSize = fs.statSync('./build/matter.dev.min.js').size;
2021-01-28 19:09:27 -05:00
console.log(
'Examples ran against previous release and current build\n\n'
2021-01-31 12:34:38 -05:00
+ logReport(build, `release`) + '\n'
+ logReport(dev, `current`) + '\n'
+ comparisonReport(dev, build, devSize, buildSize, MatterBuild.version, saveComparison)
2021-01-28 19:09:27 -05:00
);
});
2019-09-30 18:28:17 -04:00
describe(`Integration checks (${examples.length})`, () => {
test(`Examples run without throwing`, async () => {
const dev = await capturesDev;
const build = await capturesBuild;
expect(Object.keys(dev)).toEqual(examples);
expect(Object.keys(build)).toEqual(examples);
2019-09-30 18:28:17 -04:00
});
});
// Experimental regression comparison checks.
if (testComparison) {
describe(`Regression checks (${examples.length})`, () => {
2019-09-30 18:28:17 -04:00
expect.extend(toMatchExtrinsics);
expect.extend(toMatchIntrinsics);
test(`Examples match intrinsic properties with release build`, async () => {
const dev = await capturesDev;
const build = await capturesBuild;
// compare mass, inertia, friction etc.
expect(dev).toMatchIntrinsics(build);
2019-09-30 18:28:17 -04:00
});
test(`Examples match extrinsic positions and velocities with release build`, async () => {
const dev = await capturesDev;
const build = await capturesBuild;
// compare position, linear and angular velocity
expect(dev).toMatchExtrinsics(build);
2019-09-30 18:28:17 -04:00
});
});
}