Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logging in electron certa tests. #1361

Merged
merged 1 commit into from May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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