poku
Version:
🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.
65 lines (64 loc) • 2.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runTests = void 0;
const node_path_1 = require("path");
const node_process_1 = require("process");
const poku_js_1 = require("../configs/poku.js");
const get_arg_js_1 = require("../parsers/get-arg.js");
const os_js_1 = require("../polyfills/os.js");
const write_js_1 = require("../services/write.js");
const format_js_1 = require("./format.js");
const run_test_file_js_1 = require("./run-test-file.js");
const { cwd } = poku_js_1.GLOBAL;
if (get_arg_js_1.hasOnly)
poku_js_1.deepOptions.push('--only');
const runTests = async (files) => {
let allPassed = true;
let activeTests = 0;
let resolveDone;
const { configs } = poku_js_1.GLOBAL;
const showLogs = !configs.quiet;
const failFastError = ` ${(0, format_js_1.format)('ℹ').fail()} ${(0, format_js_1.format)('failFast').bold()} is enabled`;
const concurrency = (() => {
if (configs.sequential)
return 1;
const limit = configs.concurrency ?? Math.max((0, os_js_1.availableParallelism)() - 1, 1);
return limit <= 0 ? files.length || 1 : limit;
})();
const isSequential = concurrency === 1;
const done = new Promise((resolve) => {
resolveDone = resolve;
});
const runNext = async () => {
if (files.length === 0 && activeTests === 0) {
resolveDone(allPassed);
return;
}
const filePath = files.shift();
if (typeof filePath === 'undefined')
return;
activeTests++;
const testPassed = await (0, run_test_file_js_1.runTestFile)(filePath);
if (testPassed)
++poku_js_1.results.passed;
else {
++poku_js_1.results.failed;
allPassed = false;
if (configs.failFast) {
if (showLogs) {
(0, write_js_1.hr)();
console.error(failFastError);
(0, write_js_1.log)(`\n ${(0, format_js_1.format)('File:').bold()} ${(0, format_js_1.format)(`./${(0, node_path_1.relative)(cwd, filePath)}`).fail()}`);
(0, write_js_1.hr)();
}
(0, node_process_1.exit)(1);
}
}
activeTests--;
isSequential ? await runNext() : runNext();
};
for (let i = 0; i < concurrency; i++)
isSequential ? await runNext() : runNext();
return done;
};
exports.runTests = runTests;