From 433cbc3fc52aed149eef798c41f7efd7e3bceb43 Mon Sep 17 00:00:00 2001 From: Bill Goehrig <33036725+wgoehrig@users.noreply.github.com> Date: Mon, 10 May 2021 11:46:41 -0400 Subject: [PATCH] Fix logging in electron certa tests. Essentially just reacting to this change in mocha: https://github.com/mochajs/mocha/pull/3725 --- .../fix-certa-logging_2021-05-10-15-46.json | 11 +++++++++ .../src/runners/chrome/ChromeTestRunner.ts | 1 + .../src/runners/electron/initElectronTests.ts | 16 ++++++++----- tools/certa/src/utils/initLogging.ts | 23 +++++++++++++++++++ tools/certa/src/utils/initMocha.ts | 17 -------------- 5 files changed, 45 insertions(+), 23 deletions(-) create mode 100644 common/changes/@bentley/certa/fix-certa-logging_2021-05-10-15-46.json create mode 100644 tools/certa/src/utils/initLogging.ts diff --git a/common/changes/@bentley/certa/fix-certa-logging_2021-05-10-15-46.json b/common/changes/@bentley/certa/fix-certa-logging_2021-05-10-15-46.json new file mode 100644 index 00000000000..2ab08845e83 --- /dev/null +++ b/common/changes/@bentley/certa/fix-certa-logging_2021-05-10-15-46.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@bentley/certa", + "comment": "", + "type": "none" + } + ], + "packageName": "@bentley/certa", + "email": "33036725+wgoehrig@users.noreply.github.com" +} \ No newline at end of file diff --git a/tools/certa/src/runners/chrome/ChromeTestRunner.ts b/tools/certa/src/runners/chrome/ChromeTestRunner.ts index 699cfdb75eb..7d80d854e6f 100644 --- a/tools/certa/src/runners/chrome/ChromeTestRunner.ts +++ b/tools/certa/src/runners/chrome/ChromeTestRunner.ts @@ -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")); diff --git a/tools/certa/src/runners/electron/initElectronTests.ts b/tools/certa/src/runners/electron/initElectronTests.ts index c11c5d93175..ff86e4dfcbe 100644 --- a/tools/certa/src/runners/electron/initElectronTests.ts +++ b/tools/certa/src/runners/electron/initElectronTests.ts @@ -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) => { @@ -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) { diff --git a/tools/certa/src/utils/initLogging.ts b/tools/certa/src/utils/initLogging.ts new file mode 100644 index 00000000000..406396efcde --- /dev/null +++ b/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"); +} diff --git a/tools/certa/src/utils/initMocha.ts b/tools/certa/src/utils/initMocha.ts index 683d0723269..1660b8dbdb5 100644 --- a/tools/certa/src/utils/initMocha.ts +++ b/tools/certa/src/utils/initMocha.ts @@ -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;