UNPKG

poku

Version:

🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.

99 lines (97 loc) 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runTestFile = void 0; const node_child_process_1 = require("child_process"); const node_path_1 = require("path"); const node_process_1 = require("process"); const poku_js_1 = require("../configs/poku.js"); const get_runner_js_1 = require("../parsers/get-runner.js"); const os_js_1 = require("../parsers/os.js"); const output_js_1 = require("../parsers/output.js"); const each_js_1 = require("./each.js"); const runTestFile = async (path) => { const { cwd, configs, reporter } = poku_js_1.GLOBAL; const runtimeOptions = (0, get_runner_js_1.runner)(path); const runtime = runtimeOptions.shift(); const runtimeArguments = [ ...runtimeOptions, configs.deno?.cjs === true || (Array.isArray(configs.deno?.cjs) && configs.deno.cjs.some((ext) => path.includes(ext))) ? `https://cdn.jsdelivr.net/npm/poku${poku_js_1.VERSION ? `@${poku_js_1.VERSION}` : ''}/lib/polyfills/deno.mjs` : path, ]; const file = (0, node_path_1.relative)(cwd, path); const showLogs = !configs.quiet; let output = ''; const stdOut = (data) => { output += String(data); }; const start = (0, node_process_1.hrtime)(); let end; if (!(await (0, each_js_1.beforeEach)(file))) return false; reporter.onFileStart({ path: { relative: file, absolute: path, }, }); return new Promise((resolve) => { const child = (0, node_child_process_1.spawn)(runtime, [...runtimeArguments, ...poku_js_1.deepOptions], { stdio: ['inherit', 'pipe', 'pipe'], shell: os_js_1.isWindows, env: { ...node_process_1.env, POKU_FILE: file, POKU_RUNTIME: node_process_1.env.POKU_RUNTIME, POKU_REPORTER: configs.reporter, }, }); child.stdout.setEncoding('utf8'); child.stderr.setEncoding('utf8'); child.stdout.on('data', stdOut); child.stderr.on('data', stdOut); child.on('close', async (code) => { end = (0, node_process_1.hrtime)(start); const result = code === 0; if (showLogs) { const parsedOutputs = (0, output_js_1.parserOutput)({ output, result, })?.join('\n'); const total = end[0] * 1e3 + end[1] / 1e6; reporter.onFileResult({ status: result, path: { relative: file, absolute: path, }, duration: total, output: parsedOutputs, }); } if (!(await (0, each_js_1.afterEach)(file))) { resolve(false); return; } resolve(result); }); child.on('error', (err) => { end = (0, node_process_1.hrtime)(start); const total = end[0] * 1e3 + end[1] / 1e6; if (showLogs) console.error(`Failed to start test: ${path}`, err); reporter.onFileResult({ status: false, path: { relative: file, absolute: path, }, duration: total, }); resolve(false); }); }); }; exports.runTestFile = runTestFile;