embark/lib/tests/run_tests.js

170 lines
5.3 KiB
JavaScript
Raw Normal View History

2018-05-29 13:24:45 +00:00
const async = require('async');
const Mocha = require('mocha');
const path = require('path');
2018-09-13 18:30:15 +00:00
const {runCmd} = require('../utils/utils');
const fs = require('../core/fs');
2018-06-06 15:00:30 +00:00
const assert = require('assert');
2018-06-01 17:33:11 +00:00
const Test = require('./test');
2018-06-28 18:01:44 +00:00
const EmbarkSpec = require('./reporter');
2017-07-02 04:27:14 +00:00
2018-05-29 13:24:45 +00:00
function getFilesFromDir(filePath, cb) {
fs.readdir(filePath, (err, files) => {
if (err) {
return cb(err);
}
const testFiles = files.filter((file) => {
// Only keep the .js files
// TODO: make this a configuration in embark.json
return file.substr(-3) === '.js';
}).map((file) => {
return path.join(filePath, file);
});
cb(null, testFiles);
});
}
2017-07-02 04:27:14 +00:00
2018-05-29 13:24:45 +00:00
module.exports = {
run: function (options) {
2018-06-01 17:44:49 +00:00
let failures = 0;
let filePath = options.file;
const loglevel = options.loglevel || 'warn';
2018-05-29 13:24:45 +00:00
if (!filePath) {
filePath = 'test/';
}
2018-01-15 14:51:45 +00:00
2018-05-29 13:24:45 +00:00
async.waterfall([
function checkIfDir(next) {
if (filePath.substr(-1) === '/') {
return next(null, null);
}
fs.stat(filePath, (err, stats) => {
if (err) {
return next(`File "${filePath}" doesn't exist or you don't have permission to it`.red);
}
if (stats.isDirectory()) {
return next(null, null);
}
next(null, [filePath]);
});
},
function getFiles(files, next) {
if (files) {
return next(null, files);
}
getFilesFromDir(filePath, (err, files) => {
if (err) {
console.error('Error while reading the directory');
return next(err);
}
next(null, files);
});
},
2018-06-12 19:32:38 +00:00
function setupGlobalNamespace(files, next) {
2018-06-01 17:33:11 +00:00
// TODO put default config
2018-09-17 13:12:20 +00:00
const test = new Test({loglevel, node: options.node, coverage: options.coverage});
2018-06-01 17:33:11 +00:00
global.embark = test;
2018-06-06 15:00:30 +00:00
global.assert = assert;
2018-06-01 17:33:11 +00:00
global.config = test.config.bind(test);
2018-05-29 13:24:45 +00:00
let deprecatedWarning = function () {
2018-06-18 16:46:39 +00:00
console.error(__('%s are not supported anymore', 'EmbarkSpec & deployAll').red);
2018-06-07 15:38:18 +00:00
console.info(__('You can learn about the new revamped tests here: %s', 'https://embark.status.im/docs/testing.html'.underline));
process.exit();
};
2018-06-18 16:46:39 +00:00
global.deployAll = deprecatedWarning;
global.EmbarkSpec = {};
global.EmbarkSpec.deployAll = deprecatedWarning;
2018-06-07 20:14:42 +00:00
// Override require to enable `require('Embark/contracts/contractName');`
const Module = require('module');
const originalRequire = require('module').prototype.require;
Module.prototype.require = function (requireName) {
2018-06-07 20:14:42 +00:00
if (requireName.startsWith('Embark')) {
return test.require(...arguments);
}
return originalRequire.apply(this, arguments);
};
2018-05-29 13:24:45 +00:00
// TODO: this global here might not be necessary at all
2018-06-01 17:33:11 +00:00
global.web3 = global.embark.web3;
2017-07-03 22:15:43 +00:00
global.contract = function (describeName, callback) {
2018-05-29 13:24:45 +00:00
return Mocha.describe(describeName, callback);
};
2018-06-01 17:43:43 +00:00
2018-06-12 19:32:38 +00:00
test.init((err) => {
next(err, files);
});
2018-06-01 17:44:49 +00:00
},
function executeForAllFiles(files, next) {
async.eachLimit(files, 1, (file, eachCb) => {
const mocha = new Mocha();
2018-06-28 20:14:34 +00:00
mocha.reporter(EmbarkSpec, {
events: global.embark.engine.events,
gasDetails: options.gasDetails,
2018-07-27 17:57:39 +00:00
gasLimit: 6000000
2018-06-28 20:14:34 +00:00
});
2018-09-17 13:12:20 +00:00
2018-06-01 17:44:49 +00:00
mocha.addFile(file);
mocha.suite.timeout(0);
mocha.suite.beforeAll('Wait for deploy', (done) => {
if (global.embark.needConfig) {
global.config({});
}
global.embark.onReady((err) => {
done(err);
2018-06-01 17:44:49 +00:00
});
});
mocha.run(function (fails) {
2018-06-01 17:44:49 +00:00
failures += fails;
2018-06-28 19:18:28 +00:00
mocha.suite.removeAllListeners();
2018-06-01 17:44:49 +00:00
// Mocha prints the error already
eachCb();
});
}, next);
2018-09-13 18:30:15 +00:00
},
function runCoverage(next) {
if (!options.coverage) {
return next();
}
global.embark.events.emit('tests:finished', function() {
runCmd(`${fs.embarkPath('node_modules/.bin/istanbul')} report --root .embark --format html`,
{silent: false, exitOnError: false}, (err) => {
if (err) {
return next(err);
}
2018-09-17 19:25:00 +00:00
console.log(`Coverage report created. You can find it here: ${fs.dappPath('coverage/__root__/index.html')}\n`);
const opn = require('opn');
const _next = () => { next(); };
if (options.noBrowser) {
return next();
}
2018-09-17 14:37:24 +00:00
opn(fs.dappPath('coverage/__root__/index.html'), {wait: false})
.then(() => new Promise(resolve => setTimeout(resolve, 1000)))
.then(_next, _next);
});
});
2018-05-29 13:24:45 +00:00
}
], (err) => {
if (err) {
console.error(err);
process.exit(1);
}
2018-06-01 17:44:49 +00:00
if (failures) {
2018-06-01 14:48:29 +00:00
console.error(` > Total number of failures: ${failures}`.red.bold);
} else {
console.log(' > All tests passed'.green.bold);
2018-06-01 17:44:49 +00:00
}
// Clean contracts folder for next test run
fs.remove('.embark/contracts', (_err) => {
process.exit(failures);
2017-07-02 04:27:14 +00:00
});
});
}
2017-07-02 22:03:14 +00:00
};