Skip to content

Instantly share code, notes, and snippets.

@Lakitna
Last active March 12, 2020 13:22
Show Gist options
  • Save Lakitna/4d3eff25f79528ec06dbba84529e1c12 to your computer and use it in GitHub Desktop.
Save Lakitna/4d3eff25f79528ec06dbba84529e1c12 to your computer and use it in GitHub Desktop.
const sinon = require('sinon');
const _ = require('lodash');
describe.only('CLI', function() {
beforeEach(function() {
this.originalArgv = _.cloneDeep(process.argv);
this.exitStub = sinon.stub(process, 'exit');
});
afterEach(function() {
process.argv = this.originalArgv;
process.exit.restore();
sinon.restore();
});
it('exits with 1 when no command is provided', async function() {
await cli([]);
// Help is output twice
// Process.exit(1) is called twice
expect(process.exit).to.have.been.calledOnce; // expected exit to have been called exactly once, but it was called twice
expect(process.exit).to.have.been.calledWithExactly(1);
});
it('exits with 1 when an invalid command is provided', async function() {
await cli(['iAmVeryInvalid']);
// Help is output once
// Process.exit(1) is called once
expect(process.exit).to.have.been.calledOnce;
expect(process.exit).to.have.been.calledWithExactly(1);
});
});
/**
* @param {string[]} cliArguments
*/
async function cli(cliArguments) {
process.argv = [...process.argv.slice(0, 2), ...cliArguments];
const module = '../../bin/cli';
delete require.cache[require.resolve(module)];
return await require(module);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment