Skip to content

Commit

Permalink
added global beforeEach nemo injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Feldman committed May 2, 2020
1 parent 188daeb commit 4c994b5
Show file tree
Hide file tree
Showing 11 changed files with 665 additions and 838 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module.exports = {
"env": {
"node": true,
"es6": true
"es6": true,
"mocha": true
},

"plugins": ["es6-recommended"],
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,8 +431,9 @@ Example (find this in the test configuration):
}
...
```
When `driverPerSuite: true` the global `beforeEach` hook will have a `nemo` instance injected, but not when `driverPerSuite: false`
Please note: When using the `driverPerTest` option, there will be no `nemo` instance in the `before`/`after` lifecycle
Please note: When using the `driverPerTest: true` option, there will be no reliable `nemo` instance in the `before`/`after` lifecycle
context.
## Custom CLI Options (feature incomplete)
Expand Down
3 changes: 1 addition & 2 deletions bin/_nemo
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ NemoFactory.configure({baseDirectory: path.resolve(__dirname, '../config/cli')})
if (program.baseDirectory) {
// baseDirectory case
return NemoFactory.configure(program);
}
else if (program.configFile) {
} else if (program.configFile) {
// explicit config file case
return NemoFactory.configureJS(program);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ module.exports = function (context, instances) {
table.push(...getTableRows(output));
table.push(['TOTALS', output.totals.pass, output.totals.fail, output.totals.total]);
console.log(table.toString());

let zeroExitCode = context.config.get('profiles:base:zeroExitCode') || false
let zeroExitCode = context.config.get('profiles:base:zeroExitCode') || false;
process.exitCode = zeroExitCode ? 0 : Math.min(output.totals.fail, 255);
if (context.program.exit) {
process.exit();
Expand Down
78 changes: 47 additions & 31 deletions lib/runner/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const cJSON = require('flatted');
const Mocha = require('mocha');
const debug = require('debug');
const log = debug('nemo:mocha:log');
const {v4: uuidv4} = require('uuid');

function MochaRunner(runnerConfig) {
let testResults = [];
Expand Down Expand Up @@ -47,18 +48,27 @@ function MochaRunner(runnerConfig) {
})
.then((nemoCoreConfig) => {
NemoBaseConfig.merge(nemoCoreConfig);
return NemoCore(NemoBaseConfig);
return NemoCore(NemoBaseConfig)
.then(nemo => {
nemo.instanceId = uuidv4();
log('Instantiated new nemo instance', nemo.instanceId);
return nemo;
});
});
}

function destroyNemo() {
log('destroyNemo: called');
if (this.nemo) {
return this.nemo && this.nemo.driver && this.nemo.driver.quit()
.then(function () {
log('destroyNemo: Quit driver');
return Promise.resolve();
});
if (this.nemo.driver) {
log('Quitting nemo instance', this.nemo.instanceId);
return this.nemo.driver.quit()
.then(function () {
log('destroyNemo: Quit driver');
return Promise.resolve();
});
}
log(`Nemo instance ${this.nemo.instanceId} does not have a driver to quit`);
}
}

Expand All @@ -77,44 +87,48 @@ function MochaRunner(runnerConfig) {
return Promise.resolve();
}

function beforeRun() {
const Root = runner.suite;

runnerConfig.emitter.emit('root:before');
log('beforeRun called');

if (instanceConfig.profile.conf.driverPerTest) {
log('driverPerTest %s', instanceConfig.profile.conf.driverPerTest);
Root.beforeEach(function () {
return createNemo()
.then(bindNemo.bind(this));
});
if (Root._beforeEach.length > 0) {
Root._beforeEach.unshift(Root._beforeEach.pop());
}
Root.afterEach(destroyNemo);
return;
}
}

function beforeSuite(Suite) {
runnerConfig.emitter.emit('suite:before');
log('suite event, suite %s, root: %s', Suite.title, Suite.root);
if (instanceConfig.profile.conf.driverPerTest) {
log('driverPerTest %s', instanceConfig.profile.conf.driverPerTest);
Suite.beforeEach(function () {
if (!instanceConfig.profile.conf.driverPerTest) {
Suite.beforeAll(function () {
if (Suite.tests.length > 0) {
return createNemo()
.then(bindNemo.bind(this));
}
return;
});
if (Suite._beforeEach.length > 0) {
Suite._beforeEach.unshift(Suite._beforeEach.pop());
}
Suite.afterEach(destroyNemo);
return;
}
// createNemo beforeAll (one nemo per suite)
Suite.beforeAll(function () {
if (Suite.tests.length > 0) {
return createNemo()
.then(bindNemo.bind(this));

if (Suite._beforeAll.length > 0) {
// add nemo's beforeAll to the FRONT of the beforeAll array
Suite._beforeAll.unshift(Suite._beforeAll.pop());
}
return;
});

if (Suite._beforeAll.length > 0) {
// add nemo's beforeAll to the FRONT of the beforeAll array
Suite._beforeAll.unshift(Suite._beforeAll.pop());
// afterAll, kill nemo
Suite.afterAll(destroyNemo);
}

// afterAll, kill nemo
Suite.afterAll(destroyNemo);

}


function afterSuite(Evt) {
runnerConfig.emitter.emit('suite');
log('suite end called for %s which is root: %s', Evt.title, Evt.root);
Expand All @@ -141,7 +155,7 @@ function MochaRunner(runnerConfig) {
console.error(err);
}
let testCircularSafe = prepareTestResult(test);
runnerConfig.emitter.emit('test:before', testCircularSafe)
runnerConfig.emitter.emit('test:before', testCircularSafe);
}
function afterEachTest(test, err) {
if (err) {
Expand Down Expand Up @@ -189,11 +203,13 @@ function MochaRunner(runnerConfig) {

log('start');
runner = mocha.run(afterMocha);
runner.on('start', beforeRun);
runner.on('test', beforeEachTest);
runner.on('pass', afterEachTest);
runner.on('fail', afterEachTest);
runner.on('suite', beforeSuite);
runner.on('suite end', afterSuite);

return mochaCompletePromise;
}

Expand Down
Loading

0 comments on commit 4c994b5

Please sign in to comment.