poku
Version:
🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.
57 lines (56 loc) • 2.22 kB
JavaScript
;
var node_path = require("node:path"), process = require("node:process"), node_url = require("node:url"), _shared = require("./_shared.cjs");
require("node:os"), require("node:child_process"), require("node:assert"), require("node:assert/strict"), require("node:fs/promises"), require("node:net");
const stdoutWrite = process.stdout.write.bind(process.stdout), cleanup = () => {
process.stdout.write = stdoutWrite, process.exitCode = 0;
}, mockProcess = (outputChunks) => {
process.stdout.write = (chunk, ..._args) => (outputChunks.push(String(chunk)), !0);
}, runTestInProcess = async (path) => {
cleanup();
const { cwd, configs, reporter } = _shared.GLOBAL, file = node_path.relative(cwd, path), showLogs = !configs.quiet, outputChunks = [], start = process.hrtime();
if (!await _shared.beforeEach(file)) return !1;
mockProcess(outputChunks), reporter.onFileStart({
path: {
relative: file,
absolute: path
}
});
let timedOut = !1, killTimer, result = !0;
try {
const testPromise = import(`${node_url.pathToFileURL(path).href}?t=${Date.now()}`);
if (configs.timeout) {
const timeoutPromise = new Promise((_, reject) => {
killTimer = setTimeout(() => {
timedOut = !0, reject(
new Error(`Timeout: test file exceeded ${configs.timeout}ms limit`)
);
}, configs.timeout);
});
await Promise.race([testPromise, timeoutPromise]);
} else await testPromise;
} catch {
timedOut && outputChunks.push(_shared.timeoutMessage(configs.timeout)), result = !1;
} finally {
process.exitCode !== 0 && (result = !1), killTimer && clearTimeout(killTimer), cleanup();
}
const end = process.hrtime(start);
if (showLogs) {
const output = outputChunks.join(""), parsedOutputs = _shared.parserOutput({
output,
result,
debug: configs.debug
})?.join(`
`), total = end[0] * 1e3 + end[1] / 1e6;
reporter.onFileResult({
status: result,
path: {
relative: file,
absolute: path
},
duration: total,
output: parsedOutputs
});
}
return await _shared.afterEach(file) ? result : !1;
};
exports.runTestInProcess = runTestInProcess;