A lot of CLI commands follow the Unix philosophy and read stdin for default input and write output to stdout by default.
cli-testing-library already makes it easy to test the latter case as the execute API provides access stdout and stderr. But to test a simple CLI command that reads stdin, one is forced to use the spawn API. This is ok, but it would very nice if an optional stdin: string parameter were supported as follows:
it('program runs successfully', async () => {
const { execute, cleanup } = await prepareEnvironment()
const { code, stdout, stderr } = await execute(
'node',
'./cli.js',
'Hello? (Hello, hello, hello) / Is there anybody in there?'
)
expect(stdout).toBe(['Just nod if you can hear me / Is there anyone home?'])
await cleanup()
});
I know this would clash with the existing optional runFrom parameter. Since your are in early alpha now might be a good time to switch a different way for users to pass in optional params, e.g. an options object, e.g.:
const { code, stdout, stderr } = await execute(
'node',
'./cli.js',
{stdin: 'Hello? (Hello, hello, hello) / Is there anybody in there?',
runFrom: './testContext'}
)
Another alternative:
const { code, stdin, stdout, stderr } = await execute(
'node',
'./cli.js'
)
// writes to and immediately closes stdin:
stdin('Hello? (Hello, hello, hello) / Is there anybody in there?')
A lot of CLI commands follow the Unix philosophy and read
stdinfor default input and write output tostdoutby default.cli-testing-libraryalready makes it easy to test the latter case as theexecuteAPI provides accessstdoutandstderr. But to test a simple CLI command that readsstdin, one is forced to use thespawnAPI. This is ok, but it would very nice if an optionalstdin: stringparameter were supported as follows:I know this would clash with the existing optional
runFromparameter. Since your are in early alpha now might be a good time to switch a different way for users to pass in optional params, e.g. an options object, e.g.:Another alternative: