Skip to content

Commit

Permalink
fix: up
Browse files Browse the repository at this point in the history
  • Loading branch information
rostik404 committed Mar 14, 2018
1 parent e5be6fb commit 20e21c9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 68 deletions.
6 changes: 3 additions & 3 deletions lib/cli/index.js
Expand Up @@ -33,7 +33,7 @@ exports.run = () => {
.option('--update-refs', 'update screenshot references or gather them if they do not exist ("assertView" command)')
.arguments('[paths...]')
.action((paths) => {
return hermione
hermione
.run(paths, {
reporters: program.reporter || defaults.reporters,
browsers: program.browser,
Expand All @@ -45,11 +45,11 @@ exports.run = () => {
.catch((err) => {
logger.error(err.stack || err);
process.exit(1);
})
.done();
});
});

hermione.extendCli(program);

program.parse(process.argv);
};

Expand Down
140 changes: 75 additions & 65 deletions test/lib/cli/index.js
Expand Up @@ -2,6 +2,7 @@

const {Command} = require('commander');
const q = require('q');
const _ = require('lodash');
const hermioneCli = require('lib/cli');
const info = require('lib/cli/info');
const defaults = require('lib/config/defaults');
Expand All @@ -12,157 +13,166 @@ const any = sinon.match.any;

describe('cli', () => {
const sandbox = sinon.sandbox.create();
let parser;
const onParse = (fn = _.noop) => {
let actionFn;
Command.prototype.parse.callsFake(function() {
parser = fn(this);

actionFn && actionFn(_.get(parser, 'args'));
});

Command.prototype.action.callsFake((fn) => actionFn = fn);
};

const run_ = async (promise = q(true)) => {
sandbox.stub(Hermione.prototype, 'run').returns(promise);
hermioneCli.run();
};

beforeEach(() => {
sandbox.stub(Hermione, 'create').returns(Object.create(Hermione.prototype));
sandbox.stub(Hermione.prototype, 'run').returns(q(true));
sandbox.stub(Hermione.prototype, 'extendCli');

sandbox.stub(logger, 'log');
sandbox.stub(logger, 'error');

sandbox.stub(process, 'exit');

sandbox.spy(Command.prototype, 'parse');
sandbox.stub(Command.prototype, 'parse');
sandbox.stub(Command.prototype, 'action');

onParse();
});

afterEach(() => sandbox.restore());

it('should show information about config overriding on "--help"', () => {
process.argv.push('--help');
hermioneCli.run();
process.argv.pop();
it('should show information about config overriding on "--help"', async () => {
onParse((parser) => parser.emit('--help'));
await run_();

assert.calledOnce(logger.log);
assert.calledWith(logger.log, info.configOverriding);
});

it('should create Hermione instance', () => {
hermioneCli.run();
it('should create Hermione instance', async () => {
await run_();

assert.calledOnce(Hermione.create);
});

it('should create Hermione without config by default', () => {
hermioneCli.run();
it('should create Hermione without config by default', async () => {
await run_();

assert.calledWith(Hermione.create, undefined);
});

it('should use config path from cli', () => {
process.argv.push('--config', '.conf.hermione.js');
hermioneCli.run();
process.argv = process.argv.slice(0, -2);
it('should use config path from cli', async () => {
onParse((parser) => _.set(parser, 'config', '.conf.hermione.js'));

await run_();

assert.calledWith(Hermione.create, '.conf.hermione.js');
});

it('should run hermione', () => {
hermioneCli.run();
it('should run hermione', async () => {
await run_();

assert.calledOnce(Hermione.prototype.run);
});

it('should run hermione with paths from args', () => {
process.argv.push('first.hermione.js', 'second.hermione.js');
hermioneCli.run();
process.argv = process.argv.slice(0, -2);
it('should run hermione with paths from args', async () => {
onParse((parser) => _.set(parser, 'args', ['first.hermione.js', 'second.hermione.js']));
await run_();

assert.calledWith(Hermione.prototype.run, ['first.hermione.js', 'second.hermione.js']);
});

it('should use default reporters when running hermione', () => {
hermioneCli.run();
it('should use default reporters when running hermione', async () => {
await run_();

assert.calledWithMatch(Hermione.prototype.run, any, {reporters: defaults.reporters});
});

it('should use reporters from cli', () => {
process.argv.push('--reporter', 'first', '--reporter', 'second');
hermioneCli.run();
process.argv = process.argv.slice(0, -4);
it('should use reporters from cli', async () => {
onParse((parser) => parser.reporter = ['first', 'second']);
await run_();

assert.calledWithMatch(Hermione.prototype.run, any, {reporters: ['first', 'second']});
});

it('should not pass any browsers if they were not specified from cli', () => {
hermioneCli.run();
it('should not pass any browsers if they were not specified from cli', async () => {
await run_();

assert.calledWithMatch(Hermione.prototype.run, any, {browsers: undefined});
});

it('should use browsers from cli', () => {
process.argv.push('--browser', 'first', '--browser', 'second');
hermioneCli.run();
process.argv = process.argv.slice(0, -4);
it('should use browsers from cli', async () => {
onParse((parser) => parser.browser = ['first', 'second']);
await run_();

assert.calledWithMatch(Hermione.prototype.run, any, {browsers: ['first', 'second']});
});

it('should not pass any grep rule if it was not specified from cli', () => {
hermioneCli.run();
it('should not pass any grep rule if it was not specified from cli', async () => {
await run_();

assert.calledWithMatch(Hermione.prototype.run, any, {grep: undefined});
});

it('should use grep rule from cli', () => {
process.argv.push('--grep', 'some-rule');
hermioneCli.run();
process.argv = process.argv.slice(0, -2);
it('should use grep rule from cli', async () => {
onParse((parser) => parser.grep = 'some-rule');
await run_();

assert.calledWithMatch(Hermione.prototype.run, any, {grep: 'some-rule'});
});

it('should use update refs mode from cli', () => {
process.argv.push('--update-refs', 'true');
hermioneCli.run();
process.argv = process.argv.slice(0, -2);
it('should use update refs mode from cli', async () => {
onParse((parser) => parser.updateRefs = true);
await run_();

assert.calledWithMatch(Hermione.prototype.run, any, {updateRefs: true});
});

it('should allow hermione to extend cli', () => {
hermioneCli.run();
it('should allow hermione to extend cli', async () => {
await run_();

assert.calledOnceWith(Hermione.prototype.extendCli, sinon.match.instanceOf(Command));
});

it('should extend cli before parse', () => {
hermioneCli.run();
it('should extend cli before parse', async () => {
await run_();

assert.callOrder(Hermione.prototype.extendCli, Command.prototype.parse);
});

it('should exit with code 0 if tests pass', () => {
Hermione.prototype.run.returns(q(true));
it('should exit with code 0 if tests pass', async () => {
await run_();

return Promise.resolve(hermioneCli.run()).then(() => assert.calledWith(process.exit, 0));
assert.calledWith(process.exit, 0);
});

it('should exit with code 1 if tests fail', () => {
Hermione.prototype.run.returns(q(false));
it('should exit with code 1 if tests fail', async () => {
await run_(q(false));

return Promise.resolve(hermioneCli.run())
.then(() => assert.calledWith(process.exit, 1));
assert.calledWith(process.exit, 1);
});

it('should exit with code 1 on reject', () => {
Hermione.prototype.run.returns(q.reject({}));
it('should exit with code 1 on reject', async () => {
await run_(q.reject({}));

return Promise.resolve(hermioneCli.run())
.then(() => assert.calledWith(process.exit, 1));
assert.calledWith(process.exit, 1);
});

it('should log an error stack on reject', () => {
Hermione.prototype.run.returns(q.reject({stack: 'some-stack'}));
it('should log an error stack on reject', async () => {
await run_(q.reject({stack: 'some-stack'}));

return Promise.resolve(hermioneCli.run())
.then(() => assert.calledWith(logger.error, 'some-stack'));
assert.calledWith(logger.error, 'some-stack');
});

it('should log an error on reject if stack does not exist', () => {
Hermione.prototype.run.returns(q.reject('some-error'));
it('should log an error on reject if stack does not exist', async () => {
await run_(q.reject('some-error'));

return Promise.resolve(hermioneCli.run())
.then(() => assert.calledWith(logger.error, 'some-error'));
assert.calledWith(logger.error, 'some-error');
});
});

0 comments on commit 20e21c9

Please sign in to comment.