forked from Authenticator-Extension/Authenticator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-runner.ts
129 lines (116 loc) · 3.26 KB
/
test-runner.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Runs tests via puppeteer. Do not compile using webpack.
import puppeteer from "puppeteer";
import path from "path";
import fs from "fs";
import { execSync } from "child_process";
import merge from "lodash/merge";
interface MochaTestResults {
total?: number;
tests?: StrippedTestResults[];
completed?: boolean;
}
interface StrippedTestResults {
title: string;
duration: number;
path: string[];
err?: string;
status: "failed" | "passed" | "pending";
}
declare global {
interface Window {
__mocha_test_results__: MochaTestResults;
}
}
interface TestDisplay {
[key: string]: TestDisplay | StrippedTestResults
}
const colors = {
reset: "\x1b[0m",
green: "\x1b[32m",
red: "\x1b[31m",
}
async function runTests() {
const puppeteerArgs: string[] = [
`--load-extension=${path.resolve(__dirname, "../test/chrome")}`,
// for CI
"--no-sandbox",
"--lang=en-US,en"
];
const browser = await puppeteer.launch({
ignoreDefaultArgs: ["--disable-extensions"],
args: puppeteerArgs,
// chrome extensions don't work in headless
headless: false,
executablePath: process.env.PUPPETEER_EXEC_PATH,
});
const mochaPage = await browser.newPage();
await mochaPage.goto(
"chrome-extension://bhghoamapcdpbohphigoooaddinpkbai/view/test.html"
);
// by setting this env var, console logging works for both components and testing
if (process.env.ENABLE_CONSOLE) {
mochaPage.on("console", consoleMessage => console.log(consoleMessage.text()));
}
const results: {
testResults: MochaTestResults;
} = await mochaPage.evaluate(() => {
return new Promise((resolve: (value: {
testResults: MochaTestResults;
}) => void) => {
window.addEventListener("testsComplete", () => {
resolve({
testResults: window.__mocha_test_results__,
});
});
if (window.__mocha_test_results__.completed) {
resolve({
testResults: window.__mocha_test_results__,
});
}
});
});
let failedTest = false;
let display: TestDisplay = {};
if (results?.testResults.tests) {
for (const test of results.testResults.tests) {
let tmp: TestDisplay = {};
test.path.reduce((acc, current, index) => {
return acc[current] = test.path.length - 1 === index ? test : {}
}, tmp);
display = merge(display, tmp);
}
}
const printDisplayTests = (display: TestDisplay | StrippedTestResults) => {
for (const key in display) {
if (typeof display[key].status === "string") {
const test = display[key];
switch (test.status) {
case "passed":
console.log(`${colors.green}✓${colors.reset} ${test.title}`);
break;
case "failed":
console.log(`${colors.red}✗ ${test.title}${colors.reset}`);
if (test.err) {
console.log(test.err)
}
failedTest = true;
break;
case "pending":
console.log(`- ${test.title}`);
break;
}
} else {
console.log(key)
console.group();
printDisplayTests(display[key]);
}
}
console.groupEnd();
}
printDisplayTests(display);
process.exit(failedTest ? 1 : 0);
}
runTests().catch(e => {
console.error(e);
process.exit(1);
});