@ohos-ports/floss 5.0.1-beta.7 → 5.0.1-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/node-runner.js +52 -10
- package/package.json +1 -1
package/lib/node-runner.js
CHANGED
|
@@ -18,7 +18,48 @@ const querystring = require("querystring");
|
|
|
18
18
|
*/
|
|
19
19
|
function runInNode(opts) {
|
|
20
20
|
return new Promise((resolve, reject) => {
|
|
21
|
+
// Keep a console restore callback so quiet mode never leaks into the host process
|
|
22
|
+
let restoreConsole = null;
|
|
21
23
|
try {
|
|
24
|
+
// Set global.options for test files that depend on it (same as renderer.js),
|
|
25
|
+
// done first so any require below can already see the options
|
|
26
|
+
global.options = opts;
|
|
27
|
+
// Load mocha BEFORE overriding console: mocha's lib/reporters/base.js
|
|
28
|
+
// captures console references at module-load time, so requiring it
|
|
29
|
+
// after the quiet-mode noop overwrite would silence the spec
|
|
30
|
+
// reporter's "passing/failing" output as well
|
|
31
|
+
try {
|
|
32
|
+
require('mocha/mocha');
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
// mocha browser bundle not available, that's fine for headless mode
|
|
36
|
+
}
|
|
37
|
+
const Mocha = require('mocha');
|
|
38
|
+
// Save the original console methods; when quiet is requested, replace the
|
|
39
|
+
// documented console[log/info/error/warn/dir] methods with no-ops
|
|
40
|
+
// (mirrors upstream renderer.js setupConsoleOutput behavior)
|
|
41
|
+
const originalConsole = {
|
|
42
|
+
log: console.log,
|
|
43
|
+
info: console.info,
|
|
44
|
+
error: console.error,
|
|
45
|
+
warn: console.warn,
|
|
46
|
+
dir: console.dir,
|
|
47
|
+
};
|
|
48
|
+
const noop = () => { };
|
|
49
|
+
if (opts.quiet) {
|
|
50
|
+
console.log = noop;
|
|
51
|
+
console.info = noop;
|
|
52
|
+
console.error = noop;
|
|
53
|
+
console.warn = noop;
|
|
54
|
+
console.dir = noop;
|
|
55
|
+
}
|
|
56
|
+
restoreConsole = () => {
|
|
57
|
+
console.log = originalConsole.log;
|
|
58
|
+
console.info = originalConsole.info;
|
|
59
|
+
console.error = originalConsole.error;
|
|
60
|
+
console.warn = originalConsole.warn;
|
|
61
|
+
console.dir = originalConsole.dir;
|
|
62
|
+
};
|
|
22
63
|
// Set up jsdom environment for DOM-dependent tests
|
|
23
64
|
// jsdom provides a real DOM implementation (not a stub)
|
|
24
65
|
const { JSDOM } = require('jsdom');
|
|
@@ -37,15 +78,8 @@ function runInNode(opts) {
|
|
|
37
78
|
catch (e) {
|
|
38
79
|
// navigator is a getter in Node.js >= 21, skip it
|
|
39
80
|
}
|
|
40
|
-
// Load mocha browser support if available (sets up global mocha for bdd)
|
|
41
|
-
try {
|
|
42
|
-
require('mocha/mocha');
|
|
43
|
-
}
|
|
44
|
-
catch (e) {
|
|
45
|
-
// mocha browser bundle not available, that's fine for headless mode
|
|
46
|
-
}
|
|
47
81
|
// In headless mode, use Node.js Mocha API (same as renderer.js does)
|
|
48
|
-
|
|
82
|
+
// Mocha already required above (before console override)
|
|
49
83
|
// Parse reporter options (same logic as renderer.js)
|
|
50
84
|
let reporterOptions;
|
|
51
85
|
if (typeof opts.reporterOptions === 'string') {
|
|
@@ -97,10 +131,13 @@ function runInNode(opts) {
|
|
|
97
131
|
}
|
|
98
132
|
// Add resolved files to Mocha
|
|
99
133
|
resolvedFiles.forEach((file) => mochaInst.addFile(file));
|
|
100
|
-
// Set global.options for test files that depend on it (same as renderer.js)
|
|
101
|
-
global.options = opts;
|
|
102
134
|
// Run tests
|
|
103
135
|
mochaInst.run((errorCount) => {
|
|
136
|
+
// Restore console methods so the host process is not polluted by quiet mode
|
|
137
|
+
if (restoreConsole) {
|
|
138
|
+
restoreConsole();
|
|
139
|
+
restoreConsole = null;
|
|
140
|
+
}
|
|
104
141
|
if (errorCount > 0) {
|
|
105
142
|
// No error needed, Mocha will report this (same as renderer.js)
|
|
106
143
|
reject(new Error('Mocha tests failed.'));
|
|
@@ -111,6 +148,11 @@ function runInNode(opts) {
|
|
|
111
148
|
});
|
|
112
149
|
}
|
|
113
150
|
catch (e) {
|
|
151
|
+
// Restore console methods on failure as well
|
|
152
|
+
if (restoreConsole) {
|
|
153
|
+
restoreConsole();
|
|
154
|
+
restoreConsole = null;
|
|
155
|
+
}
|
|
114
156
|
reject(new Error(`Node.js test runner error: ${e.message}`));
|
|
115
157
|
}
|
|
116
158
|
});
|