Skip to content

Instantly share code, notes, and snippets.

@izelnakri
Created March 14, 2021 20:40
Show Gist options
  • Save izelnakri/55cf66d2d134c694d8d538ca4179089c to your computer and use it in GitHub Desktop.
Save izelnakri/55cf66d2d134c694d8d538ca4179089c to your computer and use it in GitHub Desktop.
import { assert } from 'chai';
import { Readable, PassThrough } from 'stream';
import * as readline from 'readline';
import { spawn } from 'child_process';
// Adaptor for Node 10 to adapt readline streams for async iteration.
// (via https://medium.com/@wietsevenema/node-js-using-for-await-to-read-lines-from-a-file-ead1f4dd8c6f)
function readLines(input: Readable): Readable {
let output = new PassThrough({ objectMode: true });
let lines = readline.createInterface({ input });
lines.on('line', line => output.write(line));
lines.on('close', () => output.destroy());
return output;
}
const NODE: string = process.execPath;
describe('Test a child processes', () => {
it('counts to three', async () => {
let child = spawn(NODE, ['-e', "console.log(1); console.log(2); console.log(3);"]);
let i = 0;
for await (let line of readLines(child.stdout)) {
assert.strictEqual(++i, Number(line));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment