poku
Version:
🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.
66 lines (65 loc) • 2.19 kB
JavaScript
import { relative } from "node:path";
import process from "node:process";
import { pathToFileURL } from "node:url";
import { GLOBAL, beforeEach, timeoutMessage, parserOutput, afterEach } from "./_shared.js";
import "node:os";
import "node:child_process";
import "node:assert";
import "node:assert/strict";
import "node:fs/promises";
import "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 } = GLOBAL, file = relative(cwd, path), showLogs = !configs.quiet, outputChunks = [], start = process.hrtime();
if (!await beforeEach(file)) return !1;
mockProcess(outputChunks), reporter.onFileStart({
path: {
relative: file,
absolute: path
}
});
let timedOut = !1, killTimer, result = !0;
try {
const testPromise = import(`${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(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 = 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 afterEach(file) ? result : !1;
};
export {
runTestInProcess
};