-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.mjs
161 lines (143 loc) · 5 KB
/
cli.mjs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env node
/*
@license https://github.com/t2ym/reportage/blob/master/LICENSE.md
Copyright (c) 2023 Tetsuya Mori <[email protected]>. All rights reserved.
*/
import puppeteer from 'puppeteer';
//import { KnownDevices } from 'puppeteer';
import Mocha from 'mocha';
import { reporterInstaller } from './proxy-reporter.js';
import path from 'path';
import fs from 'fs';
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
import libCoverage from 'istanbul-lib-coverage';
const argv = yargs(hideBin(process.argv))
.boolean([ 'p', 'persist' ]) // TODO: not implemented yet
.default({ 'nyc-config': './nyc.config.mjs' })
.argv;
if (argv._.length == 0) {
argv._.push('test/reportage.config.js');
}
const { default: nycConfig } = await import(argv['nyc-config']);
const imports = [];
const _noop = async () => {};
if (argv['import']) {
for (let mod of Array.isArray(argv['import']) ? argv['import'] : [ argv['import'] ]) {
/*
const { onConfig, onReady, onMochaEvent, onEnd } = await import(mod);
async onConfig({ Config });
async onReady({ Config, page, browser });
onMochaEvent({ Config, page, browser, event });
async onEnd({ Config, page, browser, event });
*/
imports.push(await import(mod));
}
}
//console.log(`argv: ${JSON.stringify(argv, null, 2)}`);
let mergedCoverageMap;
const mergeCoverage = function mergeCoverage(endEvent, Config) {
if (Config &&
Config.coverageOptions &&
Config.coverageOptions.enabled &&
endEvent &&
endEvent.detail &&
endEvent.detail.arg &&
Array.isArray(endEvent.detail.arg.__coverage__) &&
endEvent.detail.arg.__coverage__.length > 0) {
if (!mergedCoverageMap) {
mergedCoverageMap = libCoverage.createCoverageMap({});
}
for (let cov of endEvent.detail.arg.__coverage__) {
mergedCoverageMap.merge(cov);
}
}
else if (!endEvent && mergedCoverageMap) {
if (globalThis.__coverage__) {
mergedCoverageMap.merge(globalThis.__coverage__);
}
if (nycConfig['temp-dir']) {
fs.mkdirSync(path.resolve(process.cwd(), nycConfig['temp-dir']), { recursive: true });
fs.writeFileSync(path.resolve(process.cwd(), nycConfig['temp-dir'], 'out.json'), JSON.stringify(mergedCoverageMap.toJSON(), null, 2));
}
}
}
for (let configPath of argv._) {
//console.log(`configPath: ${configPath}`);
const { default: Config } = await import(
configPath.startsWith('/')
? configPath
: path.join(process.cwd(), configPath)
);
await Config.importedBy(import.meta.url);
for (const mod of imports) {
await (mod.onConfig || _noop)({ Config });
}
const reporterURL = Config.reporterURL;
//console.log('reporterURL', reporterURL);
let reporterType;
if (Mocha.reporters[Config.consoleReporter]) {
reporterType = Mocha.reporters[Config.consoleReporter];
}
else {
const { default: _reporterType } = await import(Config.consoleReporter);
reporterType = _reporterType;
}
const { ReceiverRunner } = reporterInstaller(Mocha, Config);
let browser = await puppeteer.launch(Config.puppeteerLaunchOptions);
let page = await browser.newPage();
//page.emulate(KnownDevices['Pixel 4']);
await browser.waitForTarget(target => target.type() === 'service_worker'); // wait for extension
const receiverRunner = new ReceiverRunner(reporterType);
receiverRunner.onOpen({ runner: { total: 0, stats: { } }, options: { reporterOptions: Config.consoleReporterOptions } });
let reportEnded;
const waitForReporter = new Promise(resolve => {
reportEnded = (endEvent) => {
mergeCoverage(endEvent, Config);
resolve(endEvent);
};
});
await page.exposeFunction('onMochaEvent', event => {
//console.log(event.detail);
for (const mod of imports) {
(mod.onMochaEvent || _noop)({ Config, page, browser, event });
}
receiverRunner.onMochaEvent(event.detail);
if (event.detail.type === 'end') {
reportEnded(event);
}
});
await page.evaluateOnNewDocument((new Function(`return () => {
document.addEventListener('mocha-event', event => {
window.onMochaEvent({type: 'mocha-event', detail: event.detail});
});
}`))(), 'mocha-event');
await page.goto(reporterURL, {
waitUntil: 'networkidle0',
});
await page.evaluate((new Function(`return async () => {
if (window.Dispatcher && window.Dispatcher.isReady) {
return true;
}
else {
await new Promise((resolve, reject) => {
window.addEventListener('dispatcher-ready', (event) => {
resolve(true);
});
setTimeout(() => { reject() }, 5000);
});
}
}`))());
for (const mod of imports) {
await (mod.onReady || _noop)({ Config, page, browser });
}
await page.evaluate((new Function(`return async () => {
document.querySelector('#start-button').click();
}`))());
const endEvent = await waitForReporter;
for (const mod of imports) {
await (mod.onEnd || _noop)({ Config, page, browser, event: endEvent });
}
await browser.close();
}
mergeCoverage(null);