diff --git a/src/lib/modules/tests/index.js b/src/lib/modules/tests/index.js index 20d8fa03f..82b6ea1c7 100644 --- a/src/lib/modules/tests/index.js +++ b/src/lib/modules/tests/index.js @@ -5,7 +5,7 @@ const {runCmd} = require('../../utils/utils'); const fs = require('../../core/fs'); const assert = require('assert'); const Test = require('./test'); -const EmbarkSpec = require('./reporter'); +const {EmbarkSpec, EmbarkApiSpec} = require('./reporter'); const SolcTest = require('./solc_test'); const constants = require('../../constants'); @@ -15,10 +15,33 @@ class TestRunner { this.logger = embark.logger; this.events = embark.events; this.ipc = options.ipc; + this.runResults = []; this.events.setCommandHandler('tests: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) { @@ -126,7 +149,7 @@ class TestRunner { async.waterfall([ function setupGlobalNamespace(next) { 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.assert = assert; global.config = test.config.bind(test); @@ -134,7 +157,7 @@ class TestRunner { let deprecatedWarning = function () { 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)); - process.exit(); + if(!options.inProcess) process.exit(); }; global.deployAll = deprecatedWarning; @@ -163,7 +186,8 @@ class TestRunner { let fns = files.map((file) => { return (cb) => { const mocha = new Mocha(); - mocha.reporter(EmbarkSpec, { + const reporter = options.inProcess ? EmbarkApiSpec : EmbarkSpec; + mocha.reporter(reporter, { events: self.events, gasDetails: options.gasDetails, gasLimit: constants.tests.gasLimit diff --git a/src/lib/modules/tests/reporter.js b/src/lib/modules/tests/reporter.js index de20b4695..fe41d8200 100644 --- a/src/lib/modules/tests/reporter.js +++ b/src/lib/modules/tests/reporter.js @@ -2,6 +2,33 @@ const Base = require('mocha/lib/reporters/base'); const ms = require('mocha/lib/ms'); 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 { constructor(runner, options) { super(runner, options); @@ -149,4 +176,4 @@ class EmbarkSpec extends Base { } } -module.exports = EmbarkSpec; +module.exports = {EmbarkSpec, EmbarkApiSpec}; diff --git a/src/lib/modules/tests/test.js b/src/lib/modules/tests/test.js index 382b8f644..0f71724f1 100644 --- a/src/lib/modules/tests/test.js +++ b/src/lib/modules/tests/test.js @@ -34,7 +34,7 @@ class Test { } if (!this.ipc.connected) { 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); } @@ -216,7 +216,7 @@ class Test { ], (err, accounts) => { if (err) { // 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); });