@itwin/certa
Version:
A mocha-based integration test runner
81 lines • 4.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElectronTestRunner = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/naming-convention */
const path = require("path");
const CallbackUtils_1 = require("../../utils/CallbackUtils");
const SpawnUtils_1 = require("../../utils/SpawnUtils");
class ElectronTestRunner {
static supportsCoverage = false;
static supportsCleanup = false;
static async initialize(config) {
// Restart under electron if we're running in node
if (!("electron" in process.versions))
return process.exit(await (0, SpawnUtils_1.relaunchInElectron)());
// If we are running in electron, we need to append any chromium CLI switches ***before*** the 'ready' event of the app module is emitted.
const { app } = require("electron");
if (config.debug)
app.commandLine.appendSwitch("remote-debugging-port", String(config.ports.frontendDebugging));
// Chromium no longer fallbacks to SwiftShader by default when no GPU is available, so we need to explicitly enable it.
// Since headless Electron tests run with xvfb, GPU is not reachable even if it is available on the host machine.
// More info: https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/gpu/swiftshader.md
app.commandLine.appendSwitch("enable-unsafe-swiftshader");
const timeout = new Promise((_resolve, reject) => setTimeout(() => reject(new Error("Timed out after 2 minutes when starting electron")), 2 * 60 * 1000));
await Promise.race([app.whenReady(), timeout]);
}
static async runTests(config) {
const { BrowserWindow, app, ipcMain } = require("electron");
const webPreferences = {
nodeIntegration: true,
contextIsolation: false,
sandbox: false,
};
const rendererWindow = new BrowserWindow({
show: config.debug,
webPreferences,
});
const exitElectronApp = (exitCode) => {
// Passing exit code to parent process doesn't seem to work anymore with electron 10 - sending message with status instead
// See note in SpawnUtils.onExitElectronApp
if (process.send)
process.send({ exitCode });
app.exit(exitCode);
};
ipcMain.on("certa-console", async (e, op, ...args) => {
console[op](...args);
e.returnValue = undefined; // ipcRenderer.sendSync() will hang without this
});
ipcMain.on("certa-done", (_e, count) => {
rendererWindow.webContents.once("destroyed", () => {
exitElectronApp(count);
});
setImmediate(() => rendererWindow.close());
});
ipcMain.on("certa-error", (_e, { message, stack }) => {
console.error("Uncaught Error in Tests: ", message);
console.error(stack);
rendererWindow.webContents.once("destroyed", () => {
exitElectronApp(1);
});
rendererWindow.close();
});
ipcMain.on("certa-callback", async (event, msg) => {
event.returnValue = await (0, CallbackUtils_1.executeRegisteredCallback)(msg.name, msg.args);
});
rendererWindow.webContents.once("did-finish-load", async () => {
const initScriptPath = require.resolve("./initElectronTests.js");
const startTests = async () => rendererWindow.webContents.executeJavaScript(`
var _CERTA_CONFIG = ${JSON.stringify(config)};
require(${JSON.stringify(initScriptPath)});
startCertaTests(${JSON.stringify(config.testBundle)});`);
await startTests();
});
await rendererWindow.loadFile(path.join(__dirname, "../../../public/index.html"));
}
}
exports.ElectronTestRunner = ElectronTestRunner;
//# sourceMappingURL=ElectronTestRunner.js.map