feature(@embark/tests): allow running tests via API

This commit is contained in:
Andre Medeiros 2018-12-04 11:20:58 -05:00 committed by Iuri Matias
parent 1e2cb64141
commit 168d774551
3 changed files with 58 additions and 7 deletions

View File

@ -5,7 +5,7 @@ const {runCmd} = require('../../utils/utils');
const fs = require('../../core/fs'); const fs = require('../../core/fs');
const assert = require('assert'); const assert = require('assert');
const Test = require('./test'); const Test = require('./test');
const EmbarkSpec = require('./reporter'); const {EmbarkSpec, EmbarkApiSpec} = require('./reporter');
const SolcTest = require('./solc_test'); const SolcTest = require('./solc_test');
const constants = require('../../constants'); const constants = require('../../constants');
@ -15,10 +15,33 @@ class TestRunner {
this.logger = embark.logger; this.logger = embark.logger;
this.events = embark.events; this.events = embark.events;
this.ipc = options.ipc; this.ipc = options.ipc;
this.runResults = [];
this.events.setCommandHandler('tests:run', (options, callback) => { this.events.setCommandHandler('tests:run', (options, callback) => {
this.run(options, callback); this.run(options, callback);
}); });
this.events.setCommandHandler('tests:results:reset', () => {
this.runResults = [];
});
this.events.setCommandHandler('tests:results:get', (callback) => {
callback(this.runResults);
});
this.events.setCommandHandler('tests:results:report', (test) => {
this.runResults.push(test);
});
this.embark.registerAPICall(
'post',
'/embark-api/test',
(req, res) => {
const options = {file: req.body.files, solc: true, inProcess: true};
this.run(options, () => res.send(this.runResults));
}
);
} }
run(options, cb) { run(options, cb) {
@ -126,7 +149,7 @@ class TestRunner {
async.waterfall([ async.waterfall([
function setupGlobalNamespace(next) { function setupGlobalNamespace(next) {
const test = new Test({loglevel: options.loglevel, node: options.node, events: self.events, logger: self.logger, const test = new Test({loglevel: options.loglevel, node: options.node, events: self.events, logger: self.logger,
config: self.embark.config, ipc: self.ipc, coverage: options.coverage}); config: self.embark.config, ipc: self.ipc, coverage: options.coverage, inProcess: options.inProcess});
global.embark = test; global.embark = test;
global.assert = assert; global.assert = assert;
global.config = test.config.bind(test); global.config = test.config.bind(test);
@ -134,7 +157,7 @@ class TestRunner {
let deprecatedWarning = function () { let deprecatedWarning = function () {
self.logger.error(__('%s are not supported anymore', 'EmbarkSpec & deployAll').red); self.logger.error(__('%s are not supported anymore', 'EmbarkSpec & deployAll').red);
self.logger.error(__('You can learn about the new revamped tests here: %s', 'https://embark.status.im/docs/testing.html'.underline)); self.logger.error(__('You can learn about the new revamped tests here: %s', 'https://embark.status.im/docs/testing.html'.underline));
process.exit(); if(!options.inProcess) process.exit();
}; };
global.deployAll = deprecatedWarning; global.deployAll = deprecatedWarning;
@ -163,7 +186,8 @@ class TestRunner {
let fns = files.map((file) => { let fns = files.map((file) => {
return (cb) => { return (cb) => {
const mocha = new Mocha(); const mocha = new Mocha();
mocha.reporter(EmbarkSpec, { const reporter = options.inProcess ? EmbarkApiSpec : EmbarkSpec;
mocha.reporter(reporter, {
events: self.events, events: self.events,
gasDetails: options.gasDetails, gasDetails: options.gasDetails,
gasLimit: constants.tests.gasLimit gasLimit: constants.tests.gasLimit

View File

@ -2,6 +2,33 @@ const Base = require('mocha/lib/reporters/base');
const ms = require('mocha/lib/ms'); const ms = require('mocha/lib/ms');
const color = Base.color; const color = Base.color;
class EmbarkApiSpec extends Base {
constructor(runner, options) {
super(runner, options);
this.embark = {events: options.reporterOptions.events};
let suiteStack = [];
const formatTest = function(test) {
return {
suite: suiteStack,
title: test.title,
file: test.file,
duration: test.duration,
state: test.state,
speed: test.speed
};
};
runner.on('start', () => { this.embark.events.request('tests:results:reset'); });
runner.on('pass', test => { this.embark.events.request('tests:results:report', formatTest(test)); });
runner.on('fail', test => { this.embark.events.request('tests:results:report', formatTest(test)); });
runner.on('suite', suite => { if(suite.title !== '') suiteStack.push(suite.title); });
runner.on('suite end', () => suiteStack.pop());
}
}
class EmbarkSpec extends Base { class EmbarkSpec extends Base {
constructor(runner, options) { constructor(runner, options) {
super(runner, options); super(runner, options);
@ -149,4 +176,4 @@ class EmbarkSpec extends Base {
} }
} }
module.exports = EmbarkSpec; module.exports = {EmbarkSpec, EmbarkApiSpec};

View File

@ -34,7 +34,7 @@ class Test {
} }
if (!this.ipc.connected) { if (!this.ipc.connected) {
this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?"); this.engine.logger.error("Could not connect to Embark's IPC. Is embark running?");
process.exit(1); if(!this.options.inProcess) process.exit(1);
} }
return this.connectToIpcNode(callback); return this.connectToIpcNode(callback);
} }
@ -216,7 +216,7 @@ class Test {
], (err, accounts) => { ], (err, accounts) => {
if (err) { if (err) {
// TODO Do not exit in case of not a normal run (eg after a change) // TODO Do not exit in case of not a normal run (eg after a change)
process.exit(1); if(!self.options.inProcess) process.exit(1);
} }
callback(null, accounts); callback(null, accounts);
}); });