Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add easier logging. remove auto post test screenshots.. #4

Merged
merged 2 commits into from
Feb 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ $ ./bin/nemo --help
-F, --file run parallel by file
-D, --data run parallel by data
-S, --server run the nemo web server
-L, --logging <level> log level [info|error]
-X, --scaffold <path> inject an example nemo suite under <path>
--debug-brk enable node's debugger breaking on the first line
--inspect activate devtools in chrome
Expand Down
25 changes: 19 additions & 6 deletions bin/_nemo
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ var program = require('commander');
var path = require('path');
var fs = require('fs');
var cwd = process.cwd();
var server = require('../lib/server');
var scaffold = require('../lib/scaffold');
var debug = require('debug');
var log = debug('nemo:log');
var error = debug('nemo:error');
var prefontaine = require('../lib/prefontaine');
var debug;
var log;
var error;
var server;
var scaffold;
var prefontaine;

function list(val) {
return val.split(',');
Expand All @@ -34,13 +34,26 @@ program
.option('-F, --file ', 'run parallel by file')
.option('-D, --data ', 'run parallel by data')
.option('-S, --server ', 'run the nemo web server')
.option('-L, --logging <level>', 'log level [info|error]')
.option('-X, --scaffold <path>', 'inject an example nemo suite under <path>')
.option('--debug-brk', 'enable node\'s debugger breaking on the first line')
.option('--inspect', 'activate devtools in chrome')
.option('--no-timeouts', 'remove timeouts in debug/inspect use case')
.parse(process.argv);

program._name = 'nemo';
if (program.logging) {
process.env.DEBUG = (process.env.DEBUG) ? `${process.env.DEBUG},` : '';
process.env.DEBUG += (program.logging === 'info') ? 'nemo*' : 'nemo*:error';
}

// load these after the DEBUG var is set
server = require('../lib/server');
scaffold = require('../lib/scaffold');
prefontaine = require('../lib/prefontaine');
debug = require('debug');
log = debug('nemo:log');
error = debug('nemo:error');

// are we launching the server?
if (program.server) {
Expand Down
112 changes: 30 additions & 82 deletions lib/instance.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
'use strict';

module.exports = function instance(input, done, progress) {
var Nemo = require('nemo-core');
var CircularJSON = require('circular-json');
var Mocha = require('mocha');
var debug = require('debug');
var path = require('path');
var filenamify = require('filenamify');
var addContext = require('mochawesome/addContext');
var mkdirp = require('mkdirp');
var fs = require('fs');
var startMS;
var debug = require('debug');
var log = debug('nemo:instance:log');
var error = debug('nemo:instance:error');
var startMS;
var mocha;
var anyTests = false;
var baseDirectory = input.basedir;
var profileConf = input.profile.conf;
var profileData = profileConf.data;
var result = {tags: input.profile.tags, total: 0, pass: 0, fail: 0};
var driverConfig;
var runner;
Expand All @@ -33,7 +25,7 @@ module.exports = function instance(input, done, progress) {
test: CircularJSON.stringify(test),
tags: input.profile.tags
};
}
};
var exitInstance = function (failures, summary) {
// only attach this listener if we aren't running in parallel
if (summary) {
Expand All @@ -58,8 +50,9 @@ module.exports = function instance(input, done, progress) {
};

var defineSuite = function (config) {
// console.log(config);
var nemo;
var requireFromConfig = profileConf.mocha.require;
var requireFromConfig = input.profile.conf.mocha.require;
var modulesToRequire = [];

// if an array was already declared, use it
Expand All @@ -80,18 +73,18 @@ module.exports = function instance(input, done, progress) {
});

// make a Mocha
mocha = new Mocha(profileConf.mocha);
mocha = new Mocha(input.profile.conf.mocha);
// add files
profileConf.tests.forEach(function (file) {
input.profile.conf.tests.forEach(function (file) {
log('%s: add file %s', input.profile.tags.uid, file);
mocha.addFile(file);
});
// calculate driver configuration
driverConfig = profileConf.driver;
driverConfig = input.profile.conf.driver;
config.set('driver', Object.assign({}, config.get('driver'), driverConfig));
config.set('data', Object.assign({}, profileData || config.get('data')));
config.set('data', Object.assign({}, input.profile.conf.data || config.get('data')));

if (profileConf.parallel === 'file' && mocha.options.grep) {
if (input.profile.conf.parallel === 'file' && mocha.options.grep) {
// if we're running parallel by file, make sure we really need to run()
mocha.loadFiles();
const throwawayRunner = new Mocha.Runner(mocha.suite);
Expand All @@ -110,17 +103,11 @@ module.exports = function instance(input, done, progress) {
runner = mocha.run(function (failures) {
exitInstance(failures, result);
});
// runner.on('start', function (Arg) {
// // never called. filed https://github.com/mochajs/mocha/issues/2753
// });
runner.on('test', function (Test) {

Test.ctx.nemo = nemo;
nemo.mocha = Test.ctx;
});
// runner.on('test end', function (Test) {
// // not using this currently
// });
runner.on('pass', function (test) {
log('pass');
try {
Expand All @@ -132,20 +119,15 @@ module.exports = function instance(input, done, progress) {
result.pass = result.pass + 1;
});
runner.on('fail', function (test, err) {
log('fail');
error('fail', err);
progress(prepareTestResult('fail', test, err));
result.total = result.total + 1;
result.fail = result.fail + 1;
});
// runner.on('hook end', function (Evt) {
// // not using this currently
// });
runner.on('hook', function (Evt) {
Evt.ctx.nemo = nemo;
// not using this currently
});


runner.on('suite', function (Suite) {
if (input.profile.reportFile) {
result.reportFile = input.profile.reportFile;
Expand All @@ -158,44 +140,8 @@ module.exports = function instance(input, done, progress) {
Nemo(config).then(function (_nemo) {
nemo = _nemo;
nemo.runner = {
snap: function () {
var imagePath = arguments[0];
if (!imagePath) {
log(`nemo.runner.snap for test ${nemo.mocha.test.title}`);
nemo.mocha.test.__screenshot = (nemo.mocha.test.__screenshot) ? (nemo.mocha.test.__screenshot + 1) : 1;

var screenshotName = `${filenamify(nemo.mocha.test.title)}.${nemo.mocha.test.__screenshot}.png`;
imagePath = `${profileConf.reports}/${screenshotName}`;

// below only applicable for mochawesome reports
addContext(nemo.mocha, screenshotName);
}
return nemo.driver.takeScreenshot()
.then(function (img) {
let pFunk;
let p = new Promise((resolve, reject) => {
pFunk = {resolve, reject};
});
log(`afterEach: got the screenshot, ${imagePath}`);
mkdirp.sync(path.dirname(imagePath));

// save screen image
fs.writeFile(imagePath, img, {
'encoding': 'base64'
}, function (err) {
if (err) {
pFunk.reject(err);
} else {
pFunk.resolve(true);
}
});
return p;
})
.catch(function (err) {
error(`nemo.runner.snap: triggered error block: ${err}`);
});
}
}
context: input
};
_done();
}).catch(function (err) {
error(err);
Expand All @@ -207,20 +153,6 @@ module.exports = function instance(input, done, progress) {
});
Suite._beforeAll.unshift(Suite._beforeAll.pop());
}
Suite.afterEach(function capture() {
if (this.currentTest.gotScreenshot) {
return Promise.resolve();
}
this.currentTest.gotScreenshot = true;
log(`afterEach: start`);
var screenshotName = `${filenamify(this.currentTest.title)}.after.png`;
var imagePath = `${profileConf.reports}/${screenshotName}`;

// below only applicable for mochawesome reports
addContext(this, screenshotName);
return this.nemo.runner.snap(imagePath);
});
Suite._afterEach.unshift(Suite._afterEach.pop());
});
runner.on('suite end', function (Evt) {
log('suite end called for %s which is root: %s', Evt.title, Evt.root);
Expand All @@ -234,8 +166,24 @@ module.exports = function instance(input, done, progress) {
});
}
});
// currently unused mocha lifecycle events
// runner.on('start', function (Arg) {
// // never called. filed https://github.com/mochajs/mocha/issues/2753
// });
// runner.on('test end', function (Test) {
// // not using this currently
// });
// runner.on('hook end', function (Evt) {
// // not using this currently
// });
};
Nemo.Configure(baseDirectory, {}).then(defineSuite)
Nemo.Configure(input.basedir, {
plugins: {
snap: {
module: path.resolve(input.__dirname, 'snapper')
}
}
}).then(defineSuite)
.catch(function (err) {
error('error in defineSuite chain');
error(err);
Expand Down
54 changes: 54 additions & 0 deletions lib/snapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var addContext = require('mochawesome/addContext');
var path = require('path');
var mkdirp = require('mkdirp');
var fs = require('fs');
var debug = require('debug');
var log = debug('nemo:snap:log');
var error = debug('nemo:snap:error');
var filenamify = require('filenamify');


module.exports.setup = (nemo, cb) => {
log('setup is called');
nemo.snap = function () {
var imagePath = arguments[0];
var screenshotName;
if (!imagePath) {
log(`snap for test ${nemo.mocha.test.title}`);
nemo.mocha.test.__screenshot = (nemo.mocha.test.__screenshot) ? (nemo.mocha.test.__screenshot + 1) : 1;

screenshotName = `${filenamify(nemo.mocha.test.title)}.${nemo.mocha.test.__screenshot}.png`;
imagePath = `${nemo.runner.context.profile.conf.reports}/${screenshotName}`;
}
return nemo.driver.takeScreenshot()
.then(function (img) {
let pFunk;
let p = new Promise((resolve, reject) => {
pFunk = {resolve, reject};
});
log(`afterEach: got the screenshot, ${imagePath}`);
mkdirp.sync(path.dirname(imagePath));

// save screen image
fs.writeFile(imagePath, img, {
'encoding': 'base64'
}, function (err) {
if (err) {
pFunk.reject(err);
} else {
if (!screenshotName) {
screenshotName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
}
// addContext only applicable for mochawesome reports
addContext(nemo.mocha, screenshotName);
pFunk.resolve(true);
}
});
return p;
})
.catch(function (err) {
error(`triggered error block: ${err}`);
});
};
cb(null);
};
4 changes: 2 additions & 2 deletions lib/starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = function pistol() {
// TODO: below doesn't impact debuglogs of modules already loaded
process.env = Object.assign({}, process.env, iconfig.conf.env || {});
iconfig.tags.uid = uuid();
return instance({basedir: this.program.baseDirectory, profile: iconfig}, null, progress);
return instance({basedir: this.program.baseDirectory, profile: iconfig, __dirname: __dirname}, null, progress);
}
// parallel use case
tasks = this.instances.map(function (iconf) {
Expand All @@ -44,7 +44,7 @@ module.exports = function pistol() {
iconf.tags.uid = uuid();
log('multi kickoff with instance %O', iconf.tags);
thread
.send({basedir: this.program.baseDirectory, profile: iconf})
.send({basedir: this.program.baseDirectory, profile: iconf, __dirname: __dirname})
// The handlers come here: (none of them is mandatory)
.on('message', function (summary) {
log('Thread complete', summary);
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
"main": "index.js",
"scripts": {
"test": "npm run lint && npm run nemo",
"nemo": "DEBUG=nemo:storage*,nemo:instance*,nemo:starter*,mocha:runner ./bin/nemo -B test -G @suite1",
"nemo:no:selenium:promises": "SELENIUM_PROMISE_MANAGER=0 DEBUG=nemo* ./bin/nemo -B test -P pay -D",
"nemo:debug": "DEBUG=nemo:storage*,nemo:instance*,mocha:runner ./bin/nemo -B test -G @suite1 --inspect-brk --inspect",
"nemo:parallel": "DEBUG=nemsso* ./bin/nemo -B test -G @suite1,@suite2,@suite3,@suite4 -F",
"nemo:parallel:data": "DEBUG=nemso* ./bin/nemo -B test -P search,pay -D",
"nemo:search": "SELENIUM_PROMISE_MANAGER=0 DEBUG=nemo* ./bin/nemo -B test -P search -D",
"nemo": "./bin/nemo -B test -G @suite1 -L error",
"nemo:no:selenium:promises": "SELENIUM_PROMISE_MANAGER=0 ./bin/nemo -B test -P pay -D",
"nemo:debug": "./bin/nemo -B test -G @suite1 --inspect-brk --inspect",
"nemo:parallel": "./bin/nemo -B test -G @suite1,@suite2,@suite3,@suite4 -F -L error",
"nemo:parallel:data": "./bin/nemo -B test -P search,pay -D",
"nemo:search": "SELENIUM_PROMISE_MANAGER=0 ./bin/nemo -B test -P search -D",
"nemo:pay": "SELENIUM_PROMISE_MANAGER=0 ./bin/nemo -B test -P pay -D",
"nemo:trivial": "SELENIUM_PROMISE_MANAGER=0 DEBUG=nemo* ./bin/nemo -B test -P trivial -D",
"nemo:trivial:debug": "SELENIUM_PROMISE_MANAGER=0 DEBUG=nemo* ./bin/nemo -B test -P trivial -D --inspect --inspect-brk",
"nemo:form": "SELENIUM_PROMISE_MANAGER=0 DEBUG=nemo* ./bin/nemo -B test -P form",
"nemo:xunit": "DEBUG=nemo* ./bin/nemo -B test -G @suite1 -P xunit",
"nemo:server": "DEBUG=nemo* ./bin/nemo -B test -S",
"nemo:scaffold": "DEBUG=nemo* ./bin/nemo -B scaffold -P pay,search,form",
"nemo:trivial": "SELENIUM_PROMISE_MANAGER=0 ./bin/nemo -B test -P trivial -D",
"nemo:trivial:debug": "SELENIUM_PROMISE_MANAGER=0 ./bin/nemo -B test -P trivial -D --inspect --inspect-brk",
"nemo:form": "SELENIUM_PROMISE_MANAGER=0 ./bin/nemo -B test -P form",
"nemo:xunit": "./bin/nemo -B test -G @suite1 -P xunit",
"nemo:server": "./bin/nemo -B test -S",
"nemo:scaffold": "./bin/nemo -B scaffold -P pay,search,form",
"lint": "eslint ./bin/* ./lib/*"
},
"repository": {
Expand Down
3 changes: 1 addition & 2 deletions test/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
"base": {
"tests": "path:./nested*.js",
"env": {
// these only get set when running in parallel (child processes from main process)
"DEBUG": "nemo*"
// "DEBUG": "nemo*"
},
"driver": {
"builders": {
Expand Down
4 changes: 2 additions & 2 deletions test/nested.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ describe('@suite1@suite2@suite3@suite4@', function () {
}
return nemo.driver.get(nemo.data.baseUrl)
.then(function () {
return nemo.runner.snap()
return nemo.snap();
})
.then(function () {
return nemo.runner.snap()
return nemo.snap();
})
});
describe('@inner@', function () {
Expand Down