@itwin/certa
Version:
A mocha-based integration test runner
55 lines • 3.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.configureRemoteReporter = configureRemoteReporter;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const events_1 = require("events");
require("./MochaSerializer");
async function configureRemoteReporter(page) {
// This will stand in for mocha's "runner" on the backend.
// Basically, we'll just be using this to echo events from the frontend runner.
const mockRunner = new events_1.EventEmitter();
let realReporter;
// Expose a function to the frontend for initializing the reporter on the backend.
await page.exposeFunction("_CertaCreateReporter", (serializedRunner, reporterName, reporterOptions) => {
const runner = MochaSerializer.deserialize(serializedRunner);
// Reporters will use these methods to register event handlers.
// Since `runner` has been marshalled via JSON, they don't already exist.
runner.on = mockRunner.on.bind(mockRunner);
runner.once = mockRunner.once.bind(mockRunner);
// Mocha already knows how to find and construct a reporter object from a reporterName,
// so we can just leverage that instead of having to re-implement it all.
const backendMocha = new (require("mocha"))();
backendMocha.reporter(reporterName);
// Mocha.reporter saves the reporter constructor in this._reporter.
realReporter = new backendMocha._reporter(runner, { reporterOptions });
});
// Expose a function to the frontend for initializing the reporter on the backend.
await page.exposeFunction("_CertaEmitReporterEvent", (name, stats, ...args) => {
realReporter.stats = stats;
mockRunner.emit(name, ...args.map((a) => MochaSerializer.deserialize(a)));
});
await page.evaluate(() => {
(() => {
const original = mocha.reporter.bind(mocha);
mocha.reporter = (reporterName, reporterOptions) => {
// A mocha reporter class that just forwards all events to the backend.
class RemoteReporter {
constructor(runner, _options) {
const events = ["start", "end", "suite", "suite end", "test", "test end", "hook", "hook end", "pass", "fail", "pending"];
for (const event of events) {
runner.on(event, (...args) => {
window._CertaEmitReporterEvent(event, runner.stats, ...args.map((a) => MochaSerializer.serialize(a)));
});
}
window._CertaCreateReporter(MochaSerializer.serialize(runner), reporterName, reporterOptions);
}
}
return original(RemoteReporter);
};
})();
});
}
//# sourceMappingURL=MochaRemoteReporter.js.map