Skip to content

Commit

Permalink
Merge pull request #116 from gemini-testing/feature/before.file.read
Browse files Browse the repository at this point in the history
feat: BEFORE_FILE_READ and AFTER_FILE_READ events
  • Loading branch information
j0tunn authored Feb 13, 2017
2 parents ebbc915 + f2a5593 commit 523c9b2
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ sets: {
common: { // run tests associated with this path in all browsers
files: 'tests/common' // which are configured in the `browsers` option
},
desktop: {
desktop: {
files: [
'tests/desktop/*.hermione.js',
'tests/common/*.hermione.js'
Expand Down Expand Up @@ -547,6 +547,8 @@ Property name | Description

Event | Description
------------------------- | -------------
`BEFORE_FILE_READ` | Will be triggered on test files parsing before reading the file. The handler accepts data object with `file`, `browser` (browser id string) and `hermione` (helper which will be available in test file) fields.
`AFTER_FILE_READ` | Will be triggered on test files parsing right after reading the file. The handler accepts data object with `file`, `browser` (browser id string) and `hermione` (helper which will be available in test file) fields.
`RUNNER_START` | Will be triggered before test execution. If a handler returns a promise, tests will be executed only after the promise is resolved. The handler accepts an instance of a runner as the first argument. You can use this instance to emit and subscribe to any other available events.
`RUNNER_END` | Will be triggered after test execution. If a handler returns a promise, tests will be executed only after the promise is resolved.
`SESSION_START` | Will be triggered after browser session initialization. If a handler returns a promise, tests will be executed only after the promise is resolved. The handler accepts an instance of webdriverIO as the first argument and an object with a browser identifier as the second.
Expand Down
3 changes: 3 additions & 0 deletions lib/constants/runner-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const getAsyncEvents = () => ({
});

const getSyncEvents = () => ({
BEFORE_FILE_READ: 'beforeFileRead',
AFTER_FILE_READ: 'afterFileRead',

SUITE_BEGIN: 'beginSuite',
SUITE_END: 'endSuite',

Expand Down
3 changes: 3 additions & 0 deletions lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ module.exports = class MainRunner extends QEmitter {
const mochaRunner = MochaRunner.create(this._config, browserAgent, this._testSkipper);

qUtils.passthroughEvent(mochaRunner, this, [
RunnerEvents.BEFORE_FILE_READ,
RunnerEvents.AFTER_FILE_READ,

RunnerEvents.SUITE_BEGIN,
RunnerEvents.SUITE_END,

Expand Down
4 changes: 2 additions & 2 deletions lib/runner/mocha-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = class MochaRunner extends QEmitter {
.attachTestFilter(filterFn)
.attachTitleValidator(titles)
.applySkip(this._testSkipper)
.addFiles(suiteFiles)
.attachEmitFn(this.emit.bind(this));
.attachEmitFn(this.emit.bind(this))
.addFiles(suiteFiles);
}
};
9 changes: 9 additions & 0 deletions lib/runner/mocha-runner/mocha-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const logger = require('../../utils').logger;
const Skip = require('./skip/');
const SkipBuilder = require('./skip/skip-builder');
const OnlyBuilder = require('./skip/only-builder');
const RunnerEvents = require('../../constants/runner-events');
const Mocha = require('mocha');
const path = require('path');
const clearRequire = require('clear-require');
Expand Down Expand Up @@ -94,6 +95,14 @@ module.exports = class MochaAdapter {
const Reporter = _.partial(ProxyReporter, emit, () => this._getBrowser());
this._mocha.reporter(Reporter);

const emit_ = (event, file) => emit(event, {
file,
hermione: global.hermione,
browser: this._browserAgent.browserId
});
this.suite.on('pre-require', (ctx, file) => emit_(RunnerEvents.BEFORE_FILE_READ, file));
this.suite.on('post-require', (ctx, file) => emit_(RunnerEvents.AFTER_FILE_READ, file));

return this;
}

Expand Down
5 changes: 4 additions & 1 deletion test/lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ describe('Runner', () => {

describe('events', () => {
const mochaRunnerEvents = [
RunnerEvents.BEFORE_FILE_READ,
RunnerEvents.AFTER_FILE_READ,

RunnerEvents.SUITE_BEGIN,
RunnerEvents.SUITE_END,

Expand Down Expand Up @@ -243,7 +246,7 @@ describe('Runner', () => {
});
});

describe('passing of events from browser agent', () => {
describe('passing events from browser agent', () => {
beforeEach(() => sandbox.stub(BrowserAgent, 'create').returns(sinon.createStubInstance(BrowserAgent)));

[RunnerEvents.SESSION_START, RunnerEvents.SESSION_END].forEach((event) => {
Expand Down
8 changes: 8 additions & 0 deletions test/lib/runner/mocha-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ describe('mocha-runner', () => {
));
});

it('should attach emit function before file adding', () => {
return run_()
.then(() => assert.callOrder(
MochaAdapter.prototype.attachEmitFn,
MochaAdapter.prototype.addFiles
));
});

it('should call title vaidator for each file', () => {
return run_(['some/path/file.js', 'other/path/file.js'])
.then(() => {
Expand Down
27 changes: 27 additions & 0 deletions test/lib/runner/mocha-runner/mocha-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const SkipBuilder = require('../../../../lib/runner/mocha-runner/skip/skip-build
const OnlyBuilder = require('../../../../lib/runner/mocha-runner/skip/only-builder');
const Skip = require('../../../../lib/runner/mocha-runner/skip/');
const TestSkipper = require('../../../../lib/runner/test-skipper');
const RunnerEvents = require('../../../../lib/constants/runner-events');
const proxyquire = require('proxyquire').noCallThru();
const inherit = require('inherit');
const _ = require('lodash');
Expand Down Expand Up @@ -550,5 +551,31 @@ describe('mocha-runner/mocha-adapter', () => {
const getBrowser = ProxyReporter.prototype.__constructor.lastCall.args[1];
assert.deepEqual(getBrowser(), {id: 'some-browser'});
});

describe('file events', () => {
beforeEach(() => MochaAdapter.init());
afterEach(() => delete global.hermione);

_.forEach({
'pre-require': 'BEFORE_FILE_READ',
'post-require': 'AFTER_FILE_READ'
}, (hermioneEvent, mochaEvent) => {
it(`should emit ${hermioneEvent} on mocha ${mochaEvent}`, () => {
const emit = sinon.spy();
browserAgent.browserId = 'bro';

mochaAdapter.attachEmitFn(emit);

MochaStub.prototype.suite.emit(mochaEvent, {}, '/some/file.js');

assert.calledOnce(emit);
assert.calledWith(emit, RunnerEvents[hermioneEvent], sinon.match({
file: '/some/file.js',
hermione: global.hermione,
browser: 'bro'
}));
});
});
});
});
});

0 comments on commit 523c9b2

Please sign in to comment.