Skip to content

Commit

Permalink
Merge pull request #108 from gemini-testing/fix/hermione.ctx
Browse files Browse the repository at this point in the history
Fix 'hermione.ctx' method
  • Loading branch information
eGavr authored Jan 25, 2017
2 parents 3179ac4 + 8ef03de commit 988ba11
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 37 deletions.
3 changes: 3 additions & 0 deletions lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ module.exports = class MainRunner extends QEmitter {
]);

this._pool = new BrowserPool(this._config);

this.on(RunnerEvents.RUNNER_START, () => MochaRunner.init());
this.on(RunnerEvents.RUNNER_END, () => MochaRunner.clean());
}

run(tests) {
Expand Down
10 changes: 9 additions & 1 deletion lib/runner/mocha-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const QEmitter = require('qemitter');
const _ = require('lodash');

module.exports = class MochaRunner extends QEmitter {
static init() {
MochaAdapter.init();
}

static clean() {
MochaAdapter.clean();
}

static create(config, browserAgent, testSkipper) {
return new MochaRunner(config, browserAgent, testSkipper);
}
Expand All @@ -14,7 +22,7 @@ module.exports = class MochaRunner extends QEmitter {
super();

this._sharedMochaOpts = config.system.mochaOpts;
this._ctx = config.system.ctx;
this._ctx = _.clone(config.system.ctx);
this._browserAgent = browserAgent;
this._testSkipper = testSkipper;
}
Expand Down
14 changes: 10 additions & 4 deletions lib/runner/mocha-runner/mocha-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ const isSkipped = (suite) => {
};

module.exports = class MochaAdapter {
static init() {
global.hermione = {};
}

static clean() {
delete global.hermione;
}

static create(opts, browserAgent, ctx) {
return new MochaAdapter(opts, browserAgent, ctx);
}
Expand All @@ -35,14 +43,12 @@ module.exports = class MochaAdapter {
this._browser = null;

this._currentRunnable = null;
this._hermione = {ctx};

this._injectBrowser();
this._injectRunnableSpy();
this._injectSkip();

this.suite.on('pre-require', () => global.hermione = this._hermione);
this.suite.on('post-require', () => delete global.hermione);
_.extend(global.hermione, {ctx});
}

applySkip(testSkipper) {
Expand Down Expand Up @@ -104,7 +110,7 @@ module.exports = class MochaAdapter {
const skipBuilder = new SkipBuilder(skip, this._browserAgent.browserId);
const onlyBuilder = new OnlyBuilder(skipBuilder);

_.extend(this._hermione, {skip: skipBuilder, only: onlyBuilder});
_.extend(global.hermione, {skip: skipBuilder, only: onlyBuilder});

this._addEventHandler(['suite', 'test'], (runnable) => skip.handleEntity(runnable));
}
Expand Down
19 changes: 19 additions & 0 deletions test/lib/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ describe('Runner', () => {
sandbox.stub(MochaRunner.prototype, 'run');
MochaRunner.prototype.run.returns(q());

sandbox.stub(MochaRunner, 'init');
sandbox.stub(MochaRunner, 'clean');

sandbox.stub(RetryManager.prototype);
});

Expand All @@ -68,6 +71,22 @@ describe('Runner', () => {
assert.calledOnce(RetryManager.prototype.__constructor);
assert.calledWith(RetryManager.prototype.__constructor, config);
});

it('should init mocha runner on RUNNER_START event', () => {
const runner = new Runner(makeConfigStub());

runner.emit(RunnerEvents.RUNNER_START);

assert.calledOnce(MochaRunner.init);
});

it('should clean mocha runner on RUNNER_END event', () => {
const runner = new Runner(makeConfigStub());

runner.emit(RunnerEvents.RUNNER_END);

assert.calledOnce(MochaRunner.clean);
});
});

describe('run', () => {
Expand Down
18 changes: 18 additions & 0 deletions test/lib/runner/mocha-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ describe('mocha-runner', () => {
const mkMochaAdapterStub_ = () => Object.create(MochaAdapter.prototype);

beforeEach(() => {
sandbox.stub(MochaAdapter, 'init');
sandbox.stub(MochaAdapter, 'clean');
sandbox.stub(MochaAdapter.prototype, 'addFiles').returnsThis();
sandbox.stub(MochaAdapter.prototype, 'attachTestFilter').returnsThis();
sandbox.stub(MochaAdapter.prototype, 'attachTitleValidator').returnsThis();
Expand All @@ -36,6 +38,22 @@ describe('mocha-runner', () => {

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

describe('init', () => {
it('should init mocha adapter', () => {
MochaRunner.init();

assert.calledOnce(MochaAdapter.init);
});
});

describe('clean', () => {
it('should clean mocha adapter', () => {
MochaRunner.clean();

assert.calledOnce(MochaAdapter.clean);
});
});

describe('run', () => {
beforeEach(() => sandbox.stub(MochaAdapter, 'create', () => mkMochaAdapterStub_()));

Expand Down
57 changes: 25 additions & 32 deletions test/lib/runner/mocha-runner/mocha-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ describe('mocha-runner/mocha-adapter', () => {

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

describe('init', () => {
it('should add an empty hermione object to global', () => {
MochaAdapter.init();

assert.deepEqual(global.hermione, {});

delete global.hermione;
});
});

describe('clean', () => {
it('should delete hermione from global', () => {
global.hermione = 'some-global-value';

MochaAdapter.clean();

assert.isUndefined(global.hermione);
});
});

describe('constructor', () => {
it('should pass shared opts to mocha instance', () => {
mkMochaAdapter_({grep: 'foo'});
Expand Down Expand Up @@ -138,54 +158,27 @@ describe('mocha-runner/mocha-adapter', () => {
assert.deepEqual(mocha.files, []);
});

it('should add global "hermione" object on "pre-require" event', () => {
const mochaAdapter = mkMochaAdapter_();

mochaAdapter.addFiles(['path/to/file']);
MochaStub.prototype.suite.emit('pre-require');

assert.isDefined(global.hermione);
});

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

it('hermione.skip should return SkipBuilder instance', () => {
const mochaAdapter = mkMochaAdapter_();

mochaAdapter.addFiles(['path/to/file']);
MochaStub.prototype.suite.emit('pre-require');
mkMochaAdapter_();

assert.instanceOf(global.hermione.skip, SkipBuilder);
});

it('hermione.only should return OnlyBuilder instance', () => {
const mochaAdapter = mkMochaAdapter_();

mochaAdapter.addFiles(['path/to/file']);
MochaStub.prototype.suite.emit('pre-require');
mkMochaAdapter_();

assert.instanceOf(global.hermione.only, OnlyBuilder);
});

it('hermione.ctx should return passed ctx', () => {
const mochaAdapter = mkMochaAdapter_({}, {some: 'ctx'});

mochaAdapter.addFiles(['path/to/file.js']);
MochaStub.prototype.suite.emit('pre-require');
mkMochaAdapter_({}, {some: 'ctx'});

assert.deepEqual(global.hermione.ctx, {some: 'ctx'});
});

it('should remove global "hermione" object on "post-require" event', () => {
const mochaAdapter = mkMochaAdapter_();

mochaAdapter.addFiles(['path/to/file']);
MochaStub.prototype.suite.emit('pre-require');
MochaStub.prototype.suite.emit('post-require');

assert.isUndefined(global.hermione);
});
});
});

Expand Down

0 comments on commit 988ba11

Please sign in to comment.