Skip to content

Commit c05606e

Browse files
committed
v3.0
1 parent ec88ea8 commit c05606e

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

index.test.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ testRunner('TESTS for config-sets', { skip: false }, (test) => {
5656
/**
5757
* Test runner. Function to run unit tests in the console.
5858
* @author Manuel Lõhmus 2024 (MIT License)
59-
* @version 1.1.4
59+
* @version 1.1.5
6060
* [2024-12-29] adde d functionality to select tests by ID in the command line arguments (e.g. --testIDs=1 2 3)
6161
* @example `npm test '--'` or `node index.test.js`
6262
* @example `npm test '--' --help` or `node index.test.js --help`
@@ -98,7 +98,9 @@ testRunner('TESTS for config-sets', { skip: false }, (test) => {
9898
*/
9999
function testRunner(runnerName, options, cb) {
100100

101-
this?.process?.on('uncaughtException', function noop() { });
101+
var globalScope = this || globalThis;
102+
103+
globalScope?.process?.on('uncaughtException', function noop() { });
102104

103105
testRunner.testRunnerOK = true;
104106
clearTimeout(testRunner.exitTimeoutID);
@@ -126,8 +128,8 @@ The following options are supported:
126128
--testID Number of the test to run (e.g. node index.test.js --testID=1 --testID=2 --testID=3)
127129
`);
128130

129-
if (this?.process?.argv[1].endsWith(".js")) { exitPressKey(); }
130-
else { process.exit(0); }
131+
if (globalScope?.process?.argv[1].endsWith(".js")) { exitPressKey(); }
132+
else { globalScope?.process?.exit(0); }
131133

132134
return;
133135
}
@@ -282,41 +284,42 @@ The following options are supported:
282284
print_stdout();
283285
}
284286

285-
this?.process?.removeAllListeners('uncaughtException');
287+
globalScope?.process?.removeAllListeners('uncaughtException');
286288

287-
if (this?.process?.argv[1].endsWith(".js")) {
289+
if (globalScope?.process?.argv[1].endsWith(".js")) {
288290

289291
exitPressKey();
290292
}
291-
else if (this?.process) {
293+
else if (globalScope?.process) {
292294

293295
if (!testRunnerOK) { testRunner.testRunnerOK = false; }
294296

295-
testRunner.exitTimeoutID = setTimeout(function () {
297+
testRunner.exitTimeoutID = setTimeout(function (exit) {
298+
299+
exit(testRunner.testRunnerOK ? 0 : 1);
296300

297-
process.exit(testRunner.testRunnerOK ? 0 : 1);
298-
}, 100);
301+
}, 100, globalScope?.process?.exit);
299302
}
300303
}
301304
}
302305

303306
function exitPressKey() {
304307

305-
process.stdin.setRawMode(true);
306-
process.stdin.resume();
307-
process.stdin.on('data', process.exit.bind(process, testRunnerOK ? 0 : 1));
308+
globalScope?.process?.stdin.setRawMode(true);
309+
globalScope?.process?.stdin.resume();
310+
globalScope?.process?.stdin.on('data', globalScope?.process?.exit.bind(globalScope?.process, testRunnerOK ? 0 : 1));
308311

309312
console.log('Press any key to exit');
310313
}
311314

312315
function arg_options() {
313316

314-
if ("undefined" === typeof process) { return {}; }
317+
if ("undefined" === typeof globalScope?.process) { return {}; }
315318

316319
var isKey = false,
317320
key = '',
318321
values,
319-
args = process.argv
322+
args = globalScope?.process?.argv
320323
.slice(2)
321324
.join('')
322325
.split('')

0 commit comments

Comments
 (0)