Skip to content

Commit

Permalink
add log capture and reporting in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
liabru committed Jan 29, 2021
1 parent ef7c4c6 commit 7bfd3c2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Example.events = function() {

// an example of using composite events on the world
Events.on(world, 'afterAdd', function(event) {
console.log('added to world:', event.object);
// do something with event.object
});

// an example of using beforeUpdate event on an engine
Expand Down
13 changes: 8 additions & 5 deletions test/ExampleWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const stubBrowserFeatures = M => {
M.Render.run = M.Render.lookAt = noop;
M.Runner.create = M.Runner.run = noop;
M.MouseConstraint.create = M.Mouse.create = noop;
M.Common.log = M.Common.info = M.Common.warn = noop;
M.Common.info = M.Common.warn = M.Common.log;
return M;
};

Expand All @@ -24,16 +24,19 @@ const { engineCapture } = require('./TestTools');
const MatterDev = stubBrowserFeatures(require('../src/module/main'));
const MatterBuild = stubBrowserFeatures(require('../build/matter'));
const Example = require('../examples/index');
const decomp = require('poly-decomp');

const runExample = options => {
const Matter = options.useDev ? MatterDev : MatterBuild;
const consoleOriginal = global.console;
const logs = [];

global.console = { log: () => {} };
global.document = global.window = { addEventListener: () => {} };
global.decomp = decomp;
global.Matter = Matter;
global.console = {
log: (...args) => {
logs.push(args.join(' '));
}
};

reset(Matter);

Expand Down Expand Up @@ -79,13 +82,13 @@ const runExample = options => {
global.console = consoleOriginal;
global.window = undefined;
global.document = undefined;
global.decomp = undefined;
global.Matter = undefined;

return {
name: options.name,
duration: totalDuration,
overlap: overlapTotal / (overlapCount || 1),
logs,
...engineCapture(engine)
};
};
Expand Down
13 changes: 11 additions & 2 deletions test/Examples.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

jest.setTimeout(30 * 1000);

const { comparisonReport, toMatchExtrinsics, toMatchIntrinsics } = require('./TestTools');
const {
comparisonReport,
logReport,
toMatchExtrinsics,
toMatchIntrinsics
} = require('./TestTools');

const Example = require('../examples/index');
const MatterBuild = require('../build/matter');
Expand Down Expand Up @@ -47,7 +52,11 @@ afterAll(async () => {
// Report experimental capture comparison.
const dev = await capturesDev;
const build = await capturesBuild;
console.log(comparisonReport(dev, build, MatterBuild.version, saveComparison));

console.log(
logReport(dev) + '\n'
+ comparisonReport(dev, build, MatterBuild.version, saveComparison)
);
});

describe(`Integration checks (${examples.length})`, () => {
Expand Down
21 changes: 20 additions & 1 deletion test/TestTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ const equals = (a, b) => {
return true;
};

const logReport = captures => {
let report = '';

for (const capture of Object.values(captures)) {
if (!capture.logs.length) {
continue;
}

report += ` ${capture.name}\n`;

for (const log of capture.logs) {
report += ` ${log}\n`;
}
}

return report ? `Output logs on last run\n\n${report}`
: 'Output logs on last run\n\n None\n';
};

const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
const similaritys = captureSimilarityExtrinsic(capturesDev, capturesBuild);
const similarityEntries = Object.entries(similaritys);
Expand Down Expand Up @@ -284,6 +303,6 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
};

module.exports = {
engineCapture, comparisonReport,
engineCapture, comparisonReport, logReport,
toMatchExtrinsics, toMatchIntrinsics
};

0 comments on commit 7bfd3c2

Please sign in to comment.