/
initElectronTests.ts
61 lines (50 loc) · 2.15 KB
/
initElectronTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* 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) => {
const { message, stack } = error || {};
ipcRenderer.send("certa-error", { message, stack });
};
window.onunhandledrejection = (event: any) => {
const { message, stack } = event.reason;
ipcRenderer.send("certa-error", { message, stack });
};
// Initialize mocha
declare const window: any;
window.mocha = new Mocha();
import "../../utils/initMocha";
async function startCertaTests(entryPoint: string) {
try {
// Setup source maps
window.sourceMapSupport = require("source-map-support");
require("../../utils/initSourceMaps");
// Load tests
// Note that we need to use a script tag instead of `require` here - that way debuggers can break on the first statement and resolve breakpoints.
const script = document.createElement("script");
script.src = entryPoint;
const loaded = new Promise((res, rej) => {
script.onload = res;
script.onerror = rej;
});
document.head.appendChild(script);
await loaded;
// Execute tests
mocha.run((failedCount) => ipcRenderer.send("certa-done", failedCount));
} catch ({ message, stack }) {
ipcRenderer.send("certa-error", { message, stack });
}
}
const certaSendToBackend = async (name: string, args: any[]) => ipcRenderer.sendSync("certa-callback", { name, args });
// Expose some globals
window.startCertaTests = startCertaTests;
window._CertaSendToBackend = certaSendToBackend;