├── .npmignore ├── .gitignore ├── .travis.yml ├── .editorconfig ├── test ├── fixtures │ └── cli.js ├── test-ava.js ├── test-jest.test.js └── test-mocha.js ├── package.json ├── index.js └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /dist/ 3 | *.log 4 | play.js 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '12' 4 | - '10' 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /test/fixtures/cli.js: -------------------------------------------------------------------------------- 1 | var inquirer = require('inquirer'); 2 | var outputs = ['TEST-1', 'TEST-2', 'TEST-3']; 3 | 4 | inquirer.prompt({ 5 | type: 'list', 6 | name: 'q', 7 | message: 'hi', 8 | choices: [ '1', '2', '3' ] 9 | }).then(function(answers) { 10 | console.log(outputs[+answers.q - 1]); 11 | }); 12 | -------------------------------------------------------------------------------- /test/test-ava.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import run, { UP, DOWN, ENTER } from './../index'; 3 | 4 | const cliPath = __dirname + '/fixtures/cli.js'; 5 | 6 | test('press enter', async t => { 7 | const result = await run([cliPath], [ENTER]); 8 | t.regex(result, new RegExp('TEST-1', 'g')); 9 | }); 10 | 11 | test('press down, press enter', async t => { 12 | const result = await run([cliPath], [DOWN, ENTER]); 13 | t.regex(result, new RegExp('TEST-2', 'g')); 14 | }); 15 | 16 | test('press up, press enter', async t => { 17 | const result = await run([cliPath], [UP, ENTER]); 18 | t.regex(result, new RegExp('TEST-3', 'g')); 19 | }); 20 | 21 | test('press up, press down, press enter', async t => { 22 | const result = await run([cliPath], [UP, DOWN, ENTER]); 23 | t.regex(result, new RegExp('TEST-1', 'g')); 24 | }); 25 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "inquirer-test", 3 | "version": "2.0.1", 4 | "description": "functional testing for inquirer.js", 5 | "main": "index.js", 6 | "scripts": { 7 | "start": "node cli.js", 8 | "test": "ava --serial --verbose test/test-ava.js && mocha test/test-mocha.js && jest" 9 | }, 10 | "engines": { 11 | "node": ">=6.0" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/ewnd9/inquirer-test.git" 16 | }, 17 | "keywords": [ 18 | "inquirer", 19 | "test", 20 | "child_process" 21 | ], 22 | "author": "ewnd9 ", 23 | "license": "MIT", 24 | "dependencies": { 25 | "concat-stream": "^1.5.1" 26 | }, 27 | "devDependencies": { 28 | "@types/jest": "^24.9.1", 29 | "ava": "^0.23.0", 30 | "chai": "^4.1.2", 31 | "inquirer": "^4.0.0", 32 | "jest": "^25.1.0", 33 | "mocha": "^4.0.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var spawn = require('child_process').spawn; 4 | var concat = require('concat-stream'); 5 | 6 | module.exports = function(args, combo, timeout) { 7 | if (!timeout) { 8 | timeout = 200; 9 | } 10 | 11 | var proc = spawn('node', args, { stdio: [null, null, null] }); 12 | proc.stdin.setEncoding('utf-8'); 13 | 14 | var loop = function(combo) { 15 | if (combo.length > 0) { 16 | setTimeout(function() { 17 | proc.stdin.write(combo[0]); 18 | loop(combo.slice(1)); 19 | }, timeout); 20 | } else { 21 | proc.stdin.end(); 22 | } 23 | }; 24 | 25 | loop(combo); 26 | 27 | return new Promise(function(resolve) { 28 | proc.stdout.pipe(concat(function(result) { 29 | resolve(result.toString()); 30 | })); 31 | }); 32 | }; 33 | 34 | module.exports.DOWN = '\x1B\x5B\x42'; 35 | module.exports.UP = '\x1B\x5B\x41'; 36 | module.exports.ENTER = '\x0D'; 37 | -------------------------------------------------------------------------------- /test/test-jest.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | describe('inquire-test', function() { 4 | const run = require('..'); 5 | const { UP, DOWN, ENTER } = run; 6 | 7 | const cliPath = `${__dirname}/fixtures/cli.js`; 8 | 9 | test('press enter', async () => { 10 | const result = await run([cliPath], [ENTER]); 11 | expect(result).toMatch(new RegExp('TEST-1', 'g')); 12 | }); 13 | 14 | test('press down, press enter', async () => { 15 | const result = await run([cliPath], [DOWN, ENTER]); 16 | expect(result).toMatch(new RegExp('TEST-2', 'g')); 17 | }); 18 | 19 | test('press up, press enter', async () => { 20 | const result = await run([cliPath], [UP, ENTER]); 21 | expect(result).toMatch(new RegExp('TEST-3', 'g')); 22 | }); 23 | 24 | test('press up, press down, press enter', async () => { 25 | const result = await run([cliPath], [UP, DOWN, ENTER]); 26 | expect(result).toMatch(new RegExp('TEST-1', 'g')); 27 | }); 28 | }); 29 | 30 | 31 | -------------------------------------------------------------------------------- /test/test-mocha.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const { expect } = require('chai'); 4 | 5 | describe('inquire-test', function() { 6 | const run = require('../'); 7 | const { UP, DOWN, ENTER } = run; 8 | 9 | const cliPath = `${__dirname}/fixtures/cli.js`; 10 | 11 | it('press enter', done => { 12 | run([cliPath], [ENTER]) 13 | .then(result => { 14 | expect(result).to.match(new RegExp('TEST-1', 'g')); 15 | done(); 16 | }) 17 | .catch(err => done(err)); 18 | }); 19 | 20 | it('press down, press enter', done => { 21 | run([cliPath], [DOWN, ENTER]) 22 | .then(result => { 23 | expect(result).to.match(new RegExp('TEST-2', 'g')); 24 | done(); 25 | }) 26 | .catch(err => done(err)); 27 | }); 28 | 29 | it('press up, press enter', done => { 30 | run([cliPath], [UP, ENTER]) 31 | .then(result => { 32 | expect(result).to.match(new RegExp('TEST-3', 'g')); 33 | done(); 34 | }) 35 | .catch(err => done(err)); 36 | }); 37 | 38 | it('press up, press down, press enter', done => { 39 | run([cliPath], [UP, DOWN, ENTER]) 40 | .then(result => { 41 | expect(result).to.match(new RegExp('TEST-1', 'g')); 42 | done(); 43 | }) 44 | .catch(err => done(err)); 45 | }); 46 | }); 47 | 48 | 49 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # inquirer-test 2 | 3 | [![Build Status](https://travis-ci.org/ewnd9/inquirer-test.svg?branch=master)](https://travis-ci.org/ewnd9/inquirer-test) 4 | 5 | Functional testing for [inquirer.js](http://npmjs.com/package/inquirer) 6 | 7 | > Could be used to test output of commands with regexps or side effects from running a script. 8 | > 9 | > E.g. with project generators you provide combinations of control keys and text inputs and ensure 10 | > that files are generated with `fs` or other modules. 11 | 12 | ## Install 13 | 14 | ``` 15 | $ npm install inquirer-test --save-dev 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```js 21 | // cli.js 22 | 23 | const inquirer = require('inquirer'); 24 | const outputs = ['TEST-1', 'TEST-2', 'TEST-3']; 25 | 26 | inquirer.prompt({ 27 | type: 'list', 28 | name: 'q', 29 | message: 'hi', 30 | choices: [ '1', '2', '3' ] 31 | }).then(function(answers) { 32 | console.log(outputs[+answers.q - 1]); 33 | }); 34 | ``` 35 | 36 | ```js 37 | // test.js 38 | 39 | import test from 'ava'; 40 | import run, { UP, DOWN, ENTER } from 'inquirer-test'; 41 | 42 | const cliPath = __dirname + '/cli.js'; 43 | 44 | test('press enter', async t => { 45 | const result = await run([cliPath], [ENTER]); 46 | t.regex(result, new RegExp('TEST-1', 'g')); 47 | }); 48 | 49 | test('press down, press enter', async t => { 50 | const result = await run([cliPath], [DOWN, ENTER]); 51 | t.regex(result, new RegExp('TEST-2', 'g')); 52 | }); 53 | 54 | test('press up, press enter', async t => { 55 | const result = await run([cliPath], [UP, ENTER]); 56 | t.regex(result, new RegExp('TEST-3', 'g')); 57 | }); 58 | 59 | test('press press up, press down, press enter', async t => { 60 | const result = await run([cliPath], [UP, DOWN, ENTER]); 61 | t.regex(result, new RegExp('TEST-1', 'g')); 62 | }); 63 | 64 | test('run with data input', async t => { 65 | const result = await run([cliPath], ['input-1', ENTER, 'input-2', ENTER]); 66 | t.regex(result, new RegExp("username: 'input-1', password: 'input-2'", 'g')); 67 | }); 68 | ``` 69 | 70 | ## Changelog 71 | 72 | - `v2.0.0` 73 | - change cliPath to `child_process` arguments array 74 | - update to `inquirer@4` 75 | 76 | ## Related 77 | 78 | - [inquirer](https://github.com/sboudrias/Inquirer.js) 79 | - [inquirer-bluebird](https://github.com/ewnd9/inquirer-bluebird) 80 | - [inquirer-question](https://github.com/ewnd9/inquirer-question) 81 | - [inquirer-credentials](https://github.com/ewnd9/inquirer-credentials) 82 | - [inquirer-menu](https://github.com/ewnd9/inquirer-menu) 83 | 84 | ## License 85 | 86 | MIT © [ewnd9](http://ewnd9.com) 87 | --------------------------------------------------------------------------------