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 000000000000..2ab08845e83c --- /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 699cfdb75eb4..7d80d854e6fc 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 c11c5d93175e..ff86e4dfcbe7 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 000000000000..406396efcde4 --- /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 683d07232692..1660b8dbdb5f 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;