diff --git a/lib/tests/run_tests.js b/lib/tests/run_tests.js index 0ea591b1..889bb3d7 100644 --- a/lib/tests/run_tests.js +++ b/lib/tests/run_tests.js @@ -21,11 +21,14 @@ function getFilesFromDir(filePath, cb) { } module.exports = { - run: function (filePath) { + run: function(filePath) { const mocha = new Mocha(); if (!filePath) { filePath = 'test/'; } + let configOptions = { + gasPrice: 1 + }; async.waterfall([ function getFiles(next) { @@ -45,6 +48,8 @@ module.exports = { }); }, function setupGlobalNamespace(next) { + global.assert = require('assert'); + // TODO put default config const test = new Test(); global.embark = test; @@ -53,16 +58,10 @@ module.exports = { // TODO: this global here might not be necessary at all global.web3 = global.embark.web3; - mocha.suite.beforeAll('Wait for deploy', (done) => { - test.onReady(() => { - done(); - }); - }); - global.contract = function (describeName, callback) { + global.contract = function(describeName, callback) { return Mocha.describe(describeName, callback); }; - - test.init(next); + next(); } ], (err) => { if (err) { @@ -70,7 +69,7 @@ module.exports = { process.exit(1); } // Run the tests. - mocha.run(function (failures) { + let runner = mocha.run(function(failures) { // Clean contracts folder for next test run fs.remove('.embark/contracts', (_err) => { process.on('exit', function () { @@ -79,6 +78,12 @@ module.exports = { process.exit(); }); }); + + runner.on('suite', function() { + global.assert = require('assert'); + global.EmbarkSpec = new Test({simulatorOptions: configOptions}); + global.web3 = global.EmbarkSpec.web3; + }); }); } }; diff --git a/lib/tests/test.js b/lib/tests/test.js index 4e2b8e2f..0da4c6ad 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -1,10 +1,7 @@ -const async = require('async'); const Engine = require('../core/engine.js'); const TestLogger = require('./test_logger.js'); const Web3 = require('web3'); const utils = require('../utils/utils'); -const constants = require('../constants'); -const Events = require('../core/events'); function getSimulator() { try { @@ -28,10 +25,6 @@ class Test { constructor(options) { this.options = options || {}; this.simOptions = this.options.simulatorOptions || {}; - this.contracts = {}; - this.events = new Events(); - this.ready = true; - this.web3 = new Web3(); if (this.simOptions.node) { this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node)); @@ -39,10 +32,15 @@ class Test { this.sim = getSimulator(); this.web3.setProvider(this.sim.provider(this.simOptions)); } + } + + config(options) { + this.options = utils.recursiveMerge(this.options, options); + this.simOptions = this.options.simulatorOptions || {}; this.engine = new Engine({ env: this.options.env || 'test', - // TODO: config will need to detect if this is a obj + // TODO: confi will need to detect if this is a obj embarkConfig: this.options.embarkConfig || 'embark.json', interceptLogs: false }); @@ -51,117 +49,12 @@ class Test { logger: new TestLogger({logLevel: 'debug'}) }); - this.engine.startService("libraryManager"); - this.engine.startService("codeRunner"); - this.engine.startService("web3", { - web3: this.web3 - }); - this.engine.startService("deployment", { - trackContracts: false - }); - this.engine.startService("codeGenerator"); - } - - init(callback) { - this.engine.contractsManager.build(() => { - callback(); - }); - } - - onReady(callback) { - if (this.ready) { - return callback(); + if (this.simOptions.node) { + this.web3.setProvider(new this.web3.providers.HttpProvider(this.simOptions.node)); + } else { + this.sim = getSimulator(); + this.web3.setProvider(this.sim.provider(this.simOptions)); } - this.events.once('ready', () => { - callback(); - }); - } - - config(options, callback) { - if (!callback) { - callback = function () {}; - } - if (!options.contracts) { - throw new Error(__('No contracts specified in the options')); - } - this.options = utils.recursiveMerge(this.options, options); - this.simOptions = this.options.simulatorOptions || {}; - this.ready = false; - - this._deploy(options, (err, accounts) => { - this.ready = true; - this.events.emit('ready'); - if (err) { - console.error(err); - return callback(err); - } - callback(null, accounts); - }); - } - - _deploy(config, callback) { - const self = this; - async.waterfall([ - function getConfig(next) { - let _versions_default = self.engine.config.contractsConfig.versions; - self.engine.config.contractsConfig = {contracts: config.contracts, versions: _versions_default}; - self.engine.events.emit(constants.events.contractConfigChanged, self.engine.config.contractsConfig); - next(); - }, - function deploy(next) { - self.engine.deployManager.gasLimit = 6000000; - self.engine.contractsManager.gasLimit = 6000000; - self.engine.deployManager.fatalErrors = true; - self.engine.deployManager.deployOnlyOnConfig = true; - self.engine.deployManager.deployContracts(function (err) { - if (err) { - return next(err); - } - next(); - }); - }, - function getAccounts(next) { - self.web3.eth.getAccounts(function (err, accounts) { - if (err) { - return next(err); - } - self.accounts = accounts; - self.web3.eth.defaultAccount = accounts[0]; - next(); - }); - }, - function createContractObject(next) { - async.each(Object.keys(self.contracts), (contractName, eachCb) => { - const contract = self.engine.contractsManager.contracts[contractName]; - Object.assign(self.contracts[contractName], new self.web3.eth.Contract(contract.abiDefinition, contract.address, - {from: self.web3.eth.defaultAccount, gas: 6000000})); - eachCb(); - }, next); - } - ], function (err) { - if (err) { - console.log(__('terminating due to error')); - throw new Error(err); - } - callback(); - }); - } - - require(module) { - if (module.startsWith('Embark/contracts/')) { - const contractName = module.substr(17); - if (!this.engine.contractsManager.contracts[contractName]) { - throw new Error(__('No contract with the name %s', contractName)); - } - if (this.contracts[contractName]) { - return this.contracts[contractName]; - } - const contract = this.engine.contractsManager.contracts[contractName]; - this.contracts[contractName] = new this.web3.eth.Contract(contract.abiDefinition, contract.address, - {from: this.web3.eth.defaultAccount, gas: 6000000}); - return this.contracts[contractName]; - } - throw new Error(__('Unknown module %s', module)); } }