UNPKG

cli-testing-library

Version:

Simple and complete CLI testing utilities that encourage good testing practices.

120 lines (119 loc) 3.83 kB
import childProcess from "node:child_process"; import { performance } from "node:perf_hooks"; import path from "node:path"; import { fileURLToPath } from "node:url"; import stripFinalNewline from "strip-final-newline"; import { _runObservers } from "./mutation-observer.js"; import { getQueriesForElement } from "./get-queries-for-instance.js"; import userEvent from "./user-event/index.js"; import { setCurrentInstance, bindObjectFnsToInstance } from "./helpers.js"; import { fireEvent } from "./events.js"; import { getConfig } from "./config.js"; import { logCLI } from "./pretty-cli.js"; const __curDir = typeof __dirname === "undefined" ? ( // @ts-ignore ESM requires this, but it doesn't work in Node18 path.dirname(fileURLToPath(import.meta.url)) ) : __dirname; const mountedInstances = /* @__PURE__ */ new Set(); async function render(command, args = [], opts = {}) { const { cwd = __curDir, spawnOpts = {} } = opts; const exec = childProcess.spawn(command, args, { ...spawnOpts, cwd, shell: true }); let _readyPromiseInternals = null; let _resolved = false; const execOutputAPI = { __exitCode: null, _isOutputAPI: true, _isReady: new Promise( (resolve, reject) => _readyPromiseInternals = { resolve, reject } ), process: exec, // Clear buffer of stdout to do more accurate `t.regex` checks clear() { execOutputAPI.stdoutArr = []; execOutputAPI.stderrArr = []; }, debug(maxLength) { logCLI(execOutputAPI, maxLength); }, // An array of strings gathered from stdout when unable to do // `await stdout` because of inquirer interactive prompts stdoutArr: [], stderrArr: [], hasExit() { return this.__exitCode === null ? null : { exitCode: this.__exitCode }; }, getStdallStr() { return this.stderrArr.concat(this.stdoutArr).sort((a, b) => a.timestamp < b.timestamp ? -1 : 1).map((obj) => obj.contents).join("\n"); } }; mountedInstances.add(execOutputAPI); exec.stdout.on("data", (result) => { if (_readyPromiseInternals && !_resolved) { _readyPromiseInternals.resolve(); _resolved = true; } const resStr = stripFinalNewline(result); execOutputAPI.stdoutArr.push({ contents: resStr, timestamp: performance.now() }); _runObservers(); }); exec.stderr.on("data", (result) => { if (_readyPromiseInternals && !_resolved) { _readyPromiseInternals.resolve(); _resolved = true; } const resStr = stripFinalNewline(result); execOutputAPI.stderrArr.push({ contents: resStr, timestamp: performance.now() }); _runObservers(); }); exec.on("error", (result) => { if (_readyPromiseInternals) { _readyPromiseInternals.reject(result); } }); exec.on("spawn", () => { setTimeout(() => { if (_readyPromiseInternals && !_resolved) { _readyPromiseInternals.resolve(); _resolved = true; } }, getConfig().renderAwaitTime); }); exec.on("exit", (code) => { execOutputAPI.__exitCode = code ?? 0; }); setCurrentInstance(execOutputAPI); await execOutputAPI._isReady; function getStdallStr() { return this.stderrArr.concat(this.stdoutArr).sort((a, b) => a.timestamp < b.timestamp ? -1 : 1).map((obj) => obj.contents).join("\n"); } return Object.assign( execOutputAPI, { userEvent: bindObjectFnsToInstance(execOutputAPI, userEvent), getStdallStr: getStdallStr.bind(execOutputAPI) }, getQueriesForElement(execOutputAPI) ); } function cleanup() { return Promise.all(Array.from(mountedInstances).map(cleanupAtInstance)); } async function cleanupAtInstance(instance) { await fireEvent.sigkill(instance); mountedInstances.delete(instance); } export { cleanup, render }; //# sourceMappingURL=pure.js.map