cli-testing-library
Version:
Simple and complete CLI testing utilities that encourage good testing practices.
121 lines (120 loc) • 4.39 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const childProcess = require("node:child_process");
const node_perf_hooks = require("node:perf_hooks");
const path = require("node:path");
const node_url = require("node:url");
const stripFinalNewline = require("strip-final-newline");
const mutationObserver = require("./mutation-observer.cjs");
const getQueriesForInstance = require("./get-queries-for-instance.cjs");
const index = require("./user-event/index.cjs");
const helpers = require("./helpers.cjs");
const events = require("./events.cjs");
const config = require("./config.cjs");
const prettyCli = require("./pretty-cli.cjs");
var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
const __curDir = typeof __dirname === "undefined" ? (
// @ts-ignore ESM requires this, but it doesn't work in Node18
path.dirname(node_url.fileURLToPath(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("cjs/pure.cjs", document.baseURI).href))
) : __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) {
prettyCli.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: node_perf_hooks.performance.now()
});
mutationObserver._runObservers();
});
exec.stderr.on("data", (result) => {
if (_readyPromiseInternals && !_resolved) {
_readyPromiseInternals.resolve();
_resolved = true;
}
const resStr = stripFinalNewline(result);
execOutputAPI.stderrArr.push({
contents: resStr,
timestamp: node_perf_hooks.performance.now()
});
mutationObserver._runObservers();
});
exec.on("error", (result) => {
if (_readyPromiseInternals) {
_readyPromiseInternals.reject(result);
}
});
exec.on("spawn", () => {
setTimeout(() => {
if (_readyPromiseInternals && !_resolved) {
_readyPromiseInternals.resolve();
_resolved = true;
}
}, config.getConfig().renderAwaitTime);
});
exec.on("exit", (code) => {
execOutputAPI.__exitCode = code ?? 0;
});
helpers.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: helpers.bindObjectFnsToInstance(execOutputAPI, index),
getStdallStr: getStdallStr.bind(execOutputAPI)
},
getQueriesForInstance.getQueriesForElement(execOutputAPI)
);
}
function cleanup() {
return Promise.all(Array.from(mountedInstances).map(cleanupAtInstance));
}
async function cleanupAtInstance(instance) {
await events.fireEvent.sigkill(instance);
mountedInstances.delete(instance);
}
exports.cleanup = cleanup;
exports.render = render;
//# sourceMappingURL=pure.cjs.map