Functional testing for inquirer.js
Could be used to test output of commands with regexps or side effects from running a script.
E.g. with project generators you provide combinations of control keys and text inputs and ensure that files are generated with
fs
or other modules.
$ npm install inquirer-test --save-dev
// cli.js
const inquirer = require('inquirer');
const outputs = ['TEST-1', 'TEST-2', 'TEST-3'];
inquirer.prompt({
type: 'list',
name: 'q',
message: 'hi',
choices: [ '1', '2', '3' ]
}).then(function(answers) {
console.log(outputs[+answers.q - 1]);
});
// test.js
import test from 'ava';
import run, { UP, DOWN, ENTER } from 'inquirer-test';
const cliPath = __dirname + '/cli.js';
test('press enter', async t => {
const result = await run([cliPath], [ENTER]);
t.regex(result, new RegExp('TEST-1', 'g'));
});
test('press down, press enter', async t => {
const result = await run([cliPath], [DOWN, ENTER]);
t.regex(result, new RegExp('TEST-2', 'g'));
});
test('press up, press enter', async t => {
const result = await run([cliPath], [UP, ENTER]);
t.regex(result, new RegExp('TEST-3', 'g'));
});
test('press press up, press down, press enter', async t => {
const result = await run([cliPath], [UP, DOWN, ENTER]);
t.regex(result, new RegExp('TEST-1', 'g'));
});
test('run with data input', async t => {
const result = await run([cliPath], ['input-1', ENTER, 'input-2', ENTER]);
t.regex(result, new RegExp("username: 'input-1', password: 'input-2'", 'g'));
});
v2.0.0
- change cliPath to
child_process
arguments array - update to
inquirer@4
- change cliPath to
MIT © ewnd9