Skip to content

Commit

Permalink
fix: Make sync and async events pass through separately
Browse files Browse the repository at this point in the history
  • Loading branch information
up73k authored and up73k committed Feb 13, 2017
1 parent a7ec581 commit 87722ba
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 53 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ hermione.only.in('chrome');
hermione.only.notIn('ie8');
```

`hermione.only.in` will run tests only in the specified browsers and skip the rest silently.
`hermione.only.in` will run tests only in the specified browsers and skip the rest silently.

`hermione.only.notIn` will run tests in all browsers except the specified ones.


Expand Down Expand Up @@ -248,7 +249,7 @@ it('should work this way', function() {
return doSomething();
});
```
In this case, the test will be skipped all browsers **silently** except in `chrome`.
The test will be skipped all browsers **silently** except in `chrome`.

```js
hermione.only.notIn('ie9');
Expand Down
20 changes: 16 additions & 4 deletions lib/constants/runner-events.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
'use strict';

module.exports = {
const _ = require('lodash');

const getAsyncEvents = () => ({
RUNNER_START: 'startRunner',
RUNNER_END: 'endRunner',

SESSION_START: 'startSession',
SESSION_END: 'endSession',

EXIT: 'exit'
});

const getSyncEvents = () => ({
SUITE_BEGIN: 'beginSuite',
SUITE_END: 'endSuite',

Expand All @@ -23,6 +29,12 @@ module.exports = {

INFO: 'info',
WARNING: 'warning',
ERROR: 'err',
EXIT: 'exit'
};
ERROR: 'err'
});

let events = _.assign(getSyncEvents(), getAsyncEvents());

Object.defineProperty(events, 'getSync', {value: getSyncEvents, enumerable: false});
Object.defineProperty(events, 'getAsync', {value: getAsyncEvents, enumerable: false});

module.exports = events;
8 changes: 5 additions & 3 deletions lib/hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

const _ = require('lodash');
const QEmitter = require('qemitter');
const passthroughEvent = require('qemitter/utils').passthroughEvent;
const qUtils = require('qemitter/utils');
const pluginsLoader = require('plugins-loader');

const RunnerEvents = require('./constants/runner-events');
const signalHandler = require('./signal-handler');
const Runner = require('./runner');
Expand Down Expand Up @@ -47,8 +48,9 @@ module.exports = class Hermione extends QEmitter {

_.forEach(options.reporters, (reporter) => applyReporter(runner, reporter));

passthroughEvent(runner, this, _.values(this.events));
passthroughEvent(signalHandler, this, RunnerEvents.EXIT);
qUtils.passthroughEvent(runner, this, _.values(RunnerEvents.getSync()));
qUtils.passthroughEventAsync(runner, this, _.values(RunnerEvents.getAsync()));
qUtils.passthroughEventAsync(signalHandler, this, RunnerEvents.EXIT);

pluginsLoader.load(this, this.config.plugins, PREFIX);
_.extend(this._config.system.mochaOpts, {grep: options.grep});
Expand Down
16 changes: 8 additions & 8 deletions lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const _ = require('lodash');
const utils = require('q-promise-utils');
const QEmitter = require('qemitter');
const passthroughEvent = require('qemitter/utils').passthroughEvent;
const qUtils = require('qemitter/utils');

const BrowserAgent = require('../browser-agent');
const BrowserPool = require('../browser-pool');
Expand All @@ -26,7 +26,7 @@ module.exports = class MainRunner extends QEmitter {
this._testSkipper = TestSkipper.create(this._config);

this._retryMgr = new RetryManager(this._config);
passthroughEvent(this._retryMgr, this, [
qUtils.passthroughEvent(this._retryMgr, this, [
RunnerEvents.TEST_FAIL,
RunnerEvents.SUITE_FAIL,
RunnerEvents.ERROR,
Expand Down Expand Up @@ -72,12 +72,7 @@ module.exports = class MainRunner extends QEmitter {
const browserAgent = BrowserAgent.create(browserId, this._pool);
const mochaRunner = MochaRunner.create(this._config, browserAgent, this._testSkipper);

passthroughEvent(browserAgent, this, [
RunnerEvents.SESSION_START,
RunnerEvents.SESSION_END
]);

passthroughEvent(mochaRunner, this, [
qUtils.passthroughEvent(mochaRunner, this, [
RunnerEvents.SUITE_BEGIN,
RunnerEvents.SUITE_END,

Expand All @@ -91,6 +86,11 @@ module.exports = class MainRunner extends QEmitter {
RunnerEvents.WARNING
]);

qUtils.passthroughEventAsync(browserAgent, this, [
RunnerEvents.SESSION_START,
RunnerEvents.SESSION_END
]);

mochaRunner.on(RunnerEvents.TEST_FAIL, (data) => this._retryMgr.handleTestFail(data));
mochaRunner.on(RunnerEvents.SUITE_FAIL, (data) => this._retryMgr.handleSuiteFail(data));
mochaRunner.on(RunnerEvents.ERROR, (err, data) => this._retryMgr.handleError(err, data));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"plugins-loader": "1.0.1",
"q": "^2.0.0",
"q-promise-utils": "^1.0.0",
"qemitter": "^1.1.0",
"qemitter": "^2.0.0",
"teamcity-service-messages": "^0.1.6",
"urijs": "^1.17.0",
"webdriverio": "^4.6.1"
Expand Down
77 changes: 73 additions & 4 deletions test/lib/hermione.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
'use strict';

const _ = require('lodash');
const qUtils = require('qemitter/utils');
const QEmitter = require('qemitter');
const EventEmitter = require('events').EventEmitter;
const pluginsLoader = require('plugins-loader');
const q = require('q');

const Config = require('../../lib/config');
const Hermione = require('../../lib/hermione');
const RunnerEvents = require('../../lib/constants/runner-events');
Expand Down Expand Up @@ -61,7 +64,7 @@ describe('hermione', () => {
const runHermione = (paths, opts) => Hermione.create().run(paths, opts);

const mkRunnerStub_ = (runFn) => {
const runner = new EventEmitter();
const runner = new QEmitter();

runner.run = sandbox.stub(Runner.prototype, 'run', runFn && runFn.bind(null, runner));
sandbox.stub(Runner, 'create').returns(runner);
Expand Down Expand Up @@ -184,13 +187,13 @@ describe('hermione', () => {
});

describe('should passthrough', () => {
it('all runner events', () => {
it('all synchronous runner events', () => {
const runner = mkRunnerStub_();
const hermione = Hermione.create(makeConfigStub());

return hermione.run()
.then(() => {
_.forEach(_.omit(hermione.events, 'EXIT'), (event, name) => {
_.forEach(RunnerEvents.getSync(), (event, name) => {
const spy = sinon.spy().named(`${name} handler`);
hermione.on(event, spy);

Expand All @@ -201,6 +204,55 @@ describe('hermione', () => {
});
});

it('synchronous runner events before "Runner.run" called', () => {
sandbox.stub(qUtils, 'passthroughEvent');
const runner = mkRunnerStub_();
const hermione = Hermione.create(makeConfigStub());

return hermione.run()
.then(() => {
assert.calledWith(qUtils.passthroughEvent,
runner,
sinon.match.instanceOf(Hermione),
_.values(RunnerEvents.getSync())
);
assert.callOrder(qUtils.passthroughEvent, runner.run);
});
});

it('all asynchronous runner events', () => {
const runner = mkRunnerStub_();
const hermione = Hermione.create(makeConfigStub());

return hermione.run()
.then(() => {
_.forEach(RunnerEvents.getAsync(), (event, name) => {
const spy = sinon.spy().named(`${name} handler`);
hermione.on(event, spy);

runner.emitAndWait(event);

assert.calledOnce(spy);
});
});
});

it('asynchronous runner events before "Runner.run" called', () => {
sandbox.stub(qUtils, 'passthroughEventAsync');
const runner = mkRunnerStub_();
const hermione = Hermione.create(makeConfigStub());

return hermione.run()
.then(() => {
assert.calledWith(qUtils.passthroughEventAsync,
runner,
sinon.match.instanceOf(Hermione),
_.values(RunnerEvents.getAsync())
);
assert.callOrder(qUtils.passthroughEventAsync, runner.run);
});
});

it('all runner events with passed event data', () => {
const runner = mkRunnerStub_();
const hermione = Hermione.create(makeConfigStub());
Expand Down Expand Up @@ -228,11 +280,28 @@ describe('hermione', () => {
.then(() => {
hermione.on('exit', onExit);

signalHandler.emit('exit');
signalHandler.emitAndWait('exit');

assert.calledOnce(onExit);
});
});

it('exit event before "Runner.run" called', () => {
sandbox.stub(qUtils, 'passthroughEventAsync');

const runner = mkRunnerStub_();
const hermione = Hermione.create(makeConfigStub());

return hermione.run()
.then(() => {
assert.calledWith(qUtils.passthroughEventAsync,
sinon.match.instanceOf(QEmitter),
sinon.match.instanceOf(Hermione),
RunnerEvents.EXIT
);
assert.callOrder(qUtils.passthroughEventAsync, runner.run);
});
});
});
});

Expand Down
Loading

0 comments on commit 87722ba

Please sign in to comment.