This mini utility is used to help mock your CLI tools when writing tests in frameworks like Mocha or Jest. It will spawn your CLI tool, and synchronously execute an array of predefined commands you provide like arrow/enter keys, text inputs, etc.
yarn add cli-mocker --dev
or
npm install cli-mocker --save-dev
import { run } from 'cli-mocker';
const { output, lastOutput } = await run('npx my-cli-command', [/* COMMANDS */]);
Commands run synchronously, so the order of the commands that you use matters. Currently, this library supports the following inputs:
import { UP } from 'cli-mocker';
import { DOWN } from 'cli-mocker';
import { ENTER } from 'cli-mocker';
import { EXIT } from 'cli-mocker';
You can pass any string argument to your cli. For example, filling in an input prompt with "hello, world!".
import chai from 'chai';
import {
run,
UP,
DOWN,
ENTER,
EXIT
} from 'cli-mocker';
const { expect } = chai;
describe('Test CLI', function() {
it('Runs', async () => {
const { output, lastOutput } = await run('npx my-cli-command', [
// Press down arrow key
DOWN,
// Press enter
ENTER,
// Press up arrow key
UP,
// Type something
'Hello, world!',
ENTER,
// Shut down CLI tool
EXIT
]);
return expect(lastOutput).to.equal(`Some value from your CLI tool's console.log() output`);
});
})
Pull requests and feature requests are welcomed!
Husky is set up to run linting and formatting before commits.