Skip to content

Commit

Permalink
Fix logging in electron certa tests. (#1361)
Browse files Browse the repository at this point in the history
Essentially just reacting to this change in mocha: mochajs/mocha#3725
  • Loading branch information
wgoehrig committed May 10, 2021
1 parent f9d4a3b commit b823f62
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 23 deletions.
@@ -0,0 +1,11 @@
{
"changes": [
{
"packageName": "@bentley/certa",
"comment": "",
"type": "none"
}
],
"packageName": "@bentley/certa",
"email": "33036725+wgoehrig@users.noreply.github.com"
}
1 change: 1 addition & 0 deletions tools/certa/src/runners/chrome/ChromeTestRunner.ts
Expand Up @@ -97,6 +97,7 @@ async function runTestsInPuppeteer(config: CertaConfig, port: string) {
const testBundle = (config.cover && config.instrumentedTestBundle) || config.testBundle;
await page.goto(`http://localhost:${port}`);
await page.addScriptTag({ content: `var _CERTA_CONFIG = ${JSON.stringify(config)};` });
await loadScript(page, require.resolve("../../utils/initLogging.js"));
await loadScript(page, require.resolve("mocha/mocha.js"));
await loadScript(page, require.resolve("source-map-support/browser-source-map-support.js"));
await loadScript(page, require.resolve("../../utils/initSourceMaps.js"));
Expand Down
16 changes: 10 additions & 6 deletions tools/certa/src/runners/electron/initElectronTests.ts
Expand Up @@ -3,6 +3,16 @@
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
import { ipcRenderer, remote } from "electron";

// NB: This has to happen _before_ we import mocha!
window._CertaConsole = (name: string, args: any[] = [""]) => {
if (args.length === 0)
args.push("");

return remote.getGlobal("console")[name].apply(remote.getGlobal("console"), args);
};
import "../../utils/initLogging";

import Mocha = require("mocha");

window.onerror = (_message: any, _source: any, _lineno: any, _colno: any, error: any) => {
Expand All @@ -18,12 +28,6 @@ window.onunhandledrejection = (event: any) => {
// Initialize mocha
declare const window: any;
window.mocha = new Mocha();
window._CertaConsole = (name: string, args: any[] = [""]) => {
if (args.length === 0)
args.push("");

return remote.getGlobal("console")[name].apply(remote.getGlobal("console"), args);
};
import "../../utils/initMocha";

async function startCertaTests(entryPoint: string) {
Expand Down
23 changes: 23 additions & 0 deletions tools/certa/src/utils/initLogging.ts
@@ -0,0 +1,23 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
// NB: This file is not a CommonJs module - it needs to run in the browser. Do not import or export modules here!

declare let _CertaConsole: undefined | ((name: string, args: any[]) => void); // eslint-disable-line @typescript-eslint/naming-convention

// Redirect all console output back to the main (backend) process, if necessary
if (typeof _CertaConsole !== "undefined") {
function forwardConsole(name: keyof typeof console) {
const original = console[name];
console[name] = (...args: any[]) => {
_CertaConsole!(name, args);
// Also preserve the original behavior. This way, test progress is reported in both the backend _and_ frontend processes.
// This helps keep the output readable when debugging the frontend.
original.apply(console, args);
};
}
forwardConsole("log");
forwardConsole("error");
forwardConsole("dir");
}
17 changes: 0 additions & 17 deletions tools/certa/src/utils/initMocha.ts
Expand Up @@ -6,23 +6,6 @@

type CertaConfig = import("../CertaConfig").CertaConfig;
declare let _CERTA_CONFIG: CertaConfig; // eslint-disable-line @typescript-eslint/naming-convention
declare let _CertaConsole: undefined | ((name: string, args: any[]) => void); // eslint-disable-line @typescript-eslint/naming-convention

// Redirect all console output back to the main (backend) process, if necessary
if (typeof _CertaConsole !== "undefined") {
function forwardConsole(name: keyof typeof console) {
const original = console[name];
console[name] = (...args: any[]) => {
_CertaConsole!(name, args);
// Also preserve the original behavior. This way, test progress is reported in both the backend _and_ frontend processes.
// This helps keep the output readable when debugging the frontend.
original.apply(console, args);
};
}
forwardConsole("log");
forwardConsole("error");
forwardConsole("dir");
}

((config: CertaConfig) => {
const mochaOpts = config.mochaOptions;
Expand Down

0 comments on commit b823f62

Please sign in to comment.