@inquirer/testing
Version:
Inquirer testing utilities
44 lines (43 loc) • 1.54 kB
JavaScript
import { Stream } from 'node:stream';
import { stripVTControlCharacters } from 'node:util';
export class BufferedStream extends Stream.Writable {
// Expose a large column width so cli-width (used by @inquirer/core's breakLines)
// doesn't hard-wrap output at 80 columns. This prevents artificial line breaks
// that would break assertions like toContain() in tests.
columns = 10_000;
#fullOutput = '';
#chunks = [];
#rawChunks = [];
#writeCount = 0;
get writeCount() {
return this.#writeCount;
}
_write(chunk, _encoding, callback) {
const str = chunk.toString();
this.#fullOutput += str;
// Keep track of every chunk sent through.
this.#rawChunks.push(str);
// Stripping the ANSI codes here because Inquirer will push commands ANSI (like cursor move.)
// This is probably fine since we don't care about those for testing; but this could become
// an issue if we ever want to test for those.
if (stripVTControlCharacters(str).trim().length > 0) {
this.#chunks.push(str);
this.#writeCount++;
this.emit('render');
}
callback();
}
getLastChunk({ raw } = {}) {
const chunks = raw ? this.#rawChunks : this.#chunks;
const lastChunk = chunks.at(-1);
return lastChunk ?? '';
}
getFullOutput() {
return this.#fullOutput;
}
clear() {
this.#fullOutput = '';
this.#chunks = [];
this.#rawChunks = [];
}
}