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

73 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-09-28 10:15:54 -04:00
/* eslint-env es6 */
/* eslint no-global-assign: 0 */
2019-09-14 14:31:31 -04:00
"use strict";
2019-09-30 18:28:17 -04:00
const {
stubBrowserFeatures, engineSnapshot, toMatchExtrinsics, toMatchIntrinsics
} = require('./TestTools');
2019-09-14 14:31:31 -04:00
2019-09-30 18:28:17 -04:00
const totalUpdates = 120;
const isCompare = process.env.COMPARE === 'true';
const excludeExamples = ['stress', 'stress2', 'svg', 'terrain'];
2019-09-14 14:31:31 -04:00
2019-09-30 18:28:17 -04:00
const MatterBuild = require('../build/matter');
const MatterDev = require('../src/module/main');
2019-09-14 14:31:31 -04:00
jest.mock('matter-wrap', () => require('../demo/lib/matter-wrap'), { virtual: true });
jest.mock('poly-decomp', () => require('../demo/lib/decomp'), { virtual: true });
2019-09-30 18:28:17 -04:00
const runExamples = (matter) => {
let snapshots = {};
matter = stubBrowserFeatures(matter);
global.Matter = matter;
matter.Plugin.register(require('matter-wrap'));
const Example = require('../examples/index');
const examples = Object.keys(Example).filter(key => !excludeExamples.includes(key));
const consoleOriginal = global.console;
global.console = { log: () => {} };
for (name of examples) {
matter.Common._nextId = matter.Common._seed = 0;
const example = Example[name]();
const engine = example.engine;
for (let i = 0; i < totalUpdates; i += 1) {
matter.Engine.update(engine, 1000 / 60);
2019-09-14 14:31:31 -04:00
}
2019-09-30 18:28:17 -04:00
snapshots[name] = isCompare ? engineSnapshot(engine) : {};
2019-09-14 14:31:31 -04:00
}
2019-09-30 18:28:17 -04:00
global.console = consoleOriginal;
global.Matter = undefined;
return snapshots;
};
const snapshotsDev = runExamples(MatterDev);
const snapshotsBuild = runExamples(MatterBuild);
const examples = Object.keys(snapshotsDev);
describe(`Integration tests (${examples.length})`, () => {
test(`Examples run without throwing`, () => {
expect(Object.keys(snapshotsDev)).toEqual(examples);
expect(Object.keys(snapshotsBuild)).toEqual(examples);
});
});
if (isCompare) {
describe(`Regression tests (${examples.length})`, () => {
expect.extend(toMatchExtrinsics);
expect.extend(toMatchIntrinsics);
test(`Examples match properties with release build`, () => {
expect(snapshotsDev).toMatchIntrinsics(snapshotsBuild, totalUpdates);
});
test(`Examples match positions and velocities with release build`, () => {
expect(snapshotsDev).toMatchExtrinsics(snapshotsBuild, totalUpdates);
});
});
}