Skip to content

Instantly share code, notes, and snippets.

@zorrodg
Last active November 30, 2023 19:37
Show Gist options
  • Star 30 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save zorrodg/c349cf54a3f6d0a9ba62e0f4066f31cb to your computer and use it in GitHub Desktop.
Save zorrodg/c349cf54a3f6d0a9ba62e0f4066f31cb to your computer and use it in GitHub Desktop.
CLI Integration Test Helper
/**
* Integration test helper
* Author: Andrés Zorro <zorrodg@gmail.com>
*/
const { existsSync } = require('fs');
const { constants } = require('os');
const spawn = require('cross-spawn');
const concat = require('concat-stream');
const PATH = process.env.PATH;
/**
* Creates a child process with script path
* @param {string} processPath Path of the process to execute
* @param {Array} args Arguments to the command
* @param {Object} env (optional) Environment variables
*/
function createProcess(processPath, args = [], env = null) {
// Ensure that path exists
if (!processPath || !existsSync(processPath)) {
throw new Error('Invalid process path');
}
args = [processPath].concat(args);
// This works for node based CLIs, but can easily be adjusted to
// any other process installed in the system
return spawn('node', args, {
env: Object.assign(
{
NODE_ENV: 'test',
preventAutoStart: false,
PATH // This is needed in order to get all the binaries in your current terminal
},
env
),
stdio: [null, null, null, 'ipc'] // This enables interprocess communication (IPC)
});
}
/**
* Creates a command and executes inputs (user responses) to the stdin
* Returns a promise that resolves when all inputs are sent
* Rejects the promise if any error
* @param {string} processPath Path of the process to execute
* @param {Array} args Arguments to the command
* @param {Array} inputs (Optional) Array of inputs (user responses)
* @param {Object} opts (optional) Environment variables
*/
function executeWithInput(processPath, args = [], inputs = [], opts = {}) {
if (!Array.isArray(inputs)) {
opts = inputs;
inputs = [];
}
const { env = null, timeout = 100, maxTimeout = 10000 } = opts;
const childProcess = createProcess(processPath, args, env);
childProcess.stdin.setEncoding('utf-8');
let currentInputTimeout, killIOTimeout;
// Creates a loop to feed user inputs to the child process in order to get results from the tool
// This code is heavily inspired (if not blantantly copied) from inquirer-test:
// https://github.com/ewnd9/inquirer-test/blob/6e2c40bbd39a061d3e52a8b1ee52cdac88f8d7f7/index.js#L14
const loop = inputs => {
if (killIOTimeout) {
clearTimeout(killIOTimeout);
}
if (!inputs.length) {
childProcess.stdin.end();
// Set a timeout to wait for CLI response. If CLI takes longer than
// maxTimeout to respond, kill the childProcess and notify user
killIOTimeout = setTimeout(() => {
console.error('Error: Reached I/O timeout');
childProcess.kill(constants.signals.SIGTERM);
}, maxTimeout);
return;
}
currentInputTimeout = setTimeout(() => {
childProcess.stdin.write(inputs[0]);
// Log debug I/O statements on tests
if (env && env.DEBUG) {
console.log('input:', inputs[0]);
}
loop(inputs.slice(1));
}, timeout);
};
const promise = new Promise((resolve, reject) => {
// Get errors from CLI
childProcess.stderr.on('data', data => {
// Log debug I/O statements on tests
if (env && env.DEBUG) {
console.log('error:', data.toString());
}
});
// Get output from CLI
childProcess.stdout.on('data', data => {
// Log debug I/O statements on tests
if (env && env.DEBUG) {
console.log('output:', data.toString());
}
});
childProcess.stderr.once('data', err => {
childProcess.stdin.end();
if (currentInputTimeout) {
clearTimeout(currentInputTimeout);
inputs = [];
}
reject(err.toString());
});
childProcess.on('error', reject);
// Kick off the process
loop(inputs);
childProcess.stdout.pipe(
concat(result => {
if (killIOTimeout) {
clearTimeout(killIOTimeout);
}
resolve(result.toString());
})
);
});
// Appending the process to the promise, in order to
// add additional parameters or behavior (such as IPC communication)
promise.attachedProcess = childProcess;
return promise;
}
module.exports = {
createProcess,
create: processPath => {
const fn = (...args) => executeWithInput(processPath, ...args);
return {
execute: fn
};
},
DOWN: '\x1B\x5B\x42',
UP: '\x1B\x5B\x41',
ENTER: '\x0D',
SPACE: '\x20'
};
MIT License
Copyright © 2019 Andrés Zorro
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@arturparkhisenko
Copy link

@zorrodg hi, do you have any plans to add the license to it? and after it maybe publish as an NPM package? :)

@thgreasi
Copy link

Yea, an npm package would be great 👍

@adam-edison
Copy link

Thanks for providing this! Awesome test helper.

@zorrodg
Copy link
Author

zorrodg commented Jun 18, 2019

Hello! Thanks for reaching out here as well. I didn’t know I had to add a license to a gist, but I’ll make sure l add one.

I don’t think I’m creating an NPM package though. My idea here is to illustrate a concept and have the user adapt it to their own specific needs.

@NicolasMassart
Copy link

Hi, I was trying to follow https://medium.com/@zorrodg/integration-tests-on-node-js-cli-part-1-why-and-how-fa5b1ba552fe but this cmd.js seems to be a bit different and when used with the pizza test from your tutorial throws an error:

1) The pizza CLI
       should print the correct output:
     TypeError: cmd.execute is not a function
      at Context.<anonymous> (test/pizza-test.js:25:32)
      at processImmediate (internal/timers.js:439:21)

Can you tell me if I should modify the test or the cmd.js?
In the tutorial, the cmd.js export part only has module.exports = { execute }; so I don't really know how to use this one. Thanks.

@xdevmaycry
Copy link

xdevmaycry commented Feb 13, 2020

Here's an example how the cmd.js library can help in your tests:

Setup

Install jest as a dev dependency

npm i -D jest

Then update your npm scripts inside package.json

  "scripts": {
    "test": "jest ./",
  },

Setup the folder structure

/lib
  cmd.js // the gist file
/bin
   __tests__
     cli.spec.js  // the test file
   cli.js // your cli         
/package.json

Example

Not tested so could be buggy

// __tests__/cli.spec.js

const path = require('path');
const cmd = require('../lib/cmd'); // import the cmd.js script from the gist

describe('my cli program', () => {
  const cliPath = path.join(__dirname, '../cli.js');
  const cliProcess = cmd.create(cliPath, '.'); // this will return a new object { execute }

  it('should print the correct output', async () => {
      const response = await cliProcess.execute( // executes the command!
        [], // args
        [], // inputs
        {}, // options 
      );
      expect(response).toEqual(''); // example test to check response is empty string
   });
});

Now run the test

npm run test

ps. This shows a rough idea of how you can utilize the cmd.js library :)

@zorrodg
Copy link
Author

zorrodg commented Feb 13, 2020

Hello @NicolasMassart. Indeed, as @xdevmaycry points out (thanks for the example code, I think it's neat), the gist contains the finalized version of the cmd function. If you go through the series up to the part 4, you will see why the function changes the API to finally land into this form.

Please, don't hesitate to ask further or contribute if you feel there's anything that can be improved.

Thanks!

@Berkmann18
Copy link

This gist would actually do a great job at being a (fairly) general CLI argument test helper/library especially since all the ones out there only test the STDIN/STDOUT And output but not with the arguments in mind.

@rluvaton
Copy link

rluvaton commented Jul 7, 2020

@zorrodg Can you PLEASE make this an NPM package

@rluvaton
Copy link

rluvaton commented Jul 7, 2020

Hello @NicolasMassart. Indeed, as @xdevmaycry points out (thanks for the example code, I think it's neat), the gist contains the finalized version of the cmd function. If you go through the series up to the part 4, you will see why the function changes the API to finally land into this form.

Please, don't hesitate to ask further or contribute if you feel there's anything that can be improved.

Thanks!

How can I change it to use some npm globally installed library?

@zorrodg
Copy link
Author

zorrodg commented Jul 7, 2020

@zorrodg Can you PLEASE make this an NPM package

Hello @rluvaton. Like I said before, I'm not really interested in publishing an npm library, as this would open a lot of untested use cases for everyone who wants to adapt this concept to their own projects. However, if you feel you can use this code without any modification, you can fork it and install it as a Gist directly. Here's an article that shows you how to do so.

How can I change it to use some npm globally installed library?

I didn't understand this question. Can you please describe your use case?

@Berkmann18
Copy link

Berkmann18 commented Jul 7, 2020

How can I change it to use some npm globally installed library?

I didn't understand this question. Can you please describe your use case?

@zorrodg If I understood what @rluvaton wrote, he was asking about how he could turn that into a package that could be globally installed (i.e. via npm i -g).

@rluvaton I haven't worked on such package (yet) but I've got that on my list, we could collaborate on this (+ anyone else interested) if you want?

@rluvaton
Copy link

rluvaton commented Jul 7, 2020

How can I change it to use some npm globally installed library?

I didn't understand this question. Can you please describe your use case?

@zorrodg If I understood what @rluvaton wrote, he was asking about how he could turn that into a package that could be globally installed (i.e. via npm i -g).

@rluvaton I haven't worked on such package (yet) but I've got that on my list, we could collaborate on this (+ anyone else interested) if you want?

@Berkmann18 We can, create a repo and add me as a collaborator

@Berkmann18
Copy link

@Berkmann18 We can, create a repo and add me as a collaborator

Done, there's a bit more of work to do on it so it can be deployed but there's a start.

@rluvaton
Copy link

rluvaton commented Jul 7, 2020

@Berkmann18 We can, create a repo and add me as a collaborator

Done, there's a bit more of work to do on it so it can be deployed but there's a start.

👍🏻 I’ll try to work on this tomorrow

@zorrodg
Copy link
Author

zorrodg commented Jul 7, 2020

Amazing guys, thanks for taking the stab at it. Like I said, I’m really not interested in covering all edge cases in an npm module, but whatever works for you!

@aptivator
Copy link

aptivator commented Nov 30, 2023

Thought I'd share a snippet that I've used in my tests. It's a bit shorter and uses output from stdout to trigger processing of the next batch of inputs. I ran the utility without setTimeout and it worked even for consecutive input operations. setTimeout is still used, but more as a precaution with a time buffer of just 5 milliseconds. Realistic interactive CLI tests are slower than normal and using very short timeouts speeds things up.

https://github.com/aptivator/anagrammer/blob/master/test/_lib/cli.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment