diff --git a/lib/tests/run_tests.js b/lib/tests/run_tests.js index 43f3a8156..0ea591b17 100644 --- a/lib/tests/run_tests.js +++ b/lib/tests/run_tests.js @@ -21,7 +21,7 @@ function getFilesFromDir(filePath, cb) { } module.exports = { - run: function(filePath) { + run: function (filePath) { const mocha = new Mocha(); if (!filePath) { filePath = 'test/'; @@ -53,7 +53,12 @@ module.exports = { // TODO: this global here might not be necessary at all global.web3 = global.embark.web3; - global.contract = function(describeName, callback) { + mocha.suite.beforeAll('Wait for deploy', (done) => { + test.onReady(() => { + done(); + }); + }); + global.contract = function (describeName, callback) { return Mocha.describe(describeName, callback); }; @@ -65,7 +70,7 @@ module.exports = { process.exit(1); } // Run the tests. - mocha.run(function(failures) { + mocha.run(function (failures) { // Clean contracts folder for next test run fs.remove('.embark/contracts', (_err) => { process.on('exit', function () { diff --git a/lib/tests/test.js b/lib/tests/test.js index a31a63c44..8049ceb09 100644 --- a/lib/tests/test.js +++ b/lib/tests/test.js @@ -4,6 +4,7 @@ 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,6 +29,8 @@ class Test { 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) { @@ -65,11 +68,26 @@ class Test { }); } + onReady(callback) { + if (this.ready) { + return callback(); + } + this.events.once('ready', () => { + callback(); + }); + } + config(options, callback) { + if (!callback) { + callback = function () {}; + } 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); diff --git a/test_apps/contracts_app/test/simple_storage_spec.js b/test_apps/contracts_app/test/simple_storage_spec.js index 71549a232..f8aeccf2a 100644 --- a/test_apps/contracts_app/test/simple_storage_spec.js +++ b/test_apps/contracts_app/test/simple_storage_spec.js @@ -1,33 +1,26 @@ -/*global contract, before, it, embark, web3*/ +/*global contract, config, it, embark*/ const assert = require('assert'); const SimpleStorage = embark.require('contracts/SimpleStorage'); +config({ + contracts: { + "SimpleStorage": { + args: [100] + } + } +}); + contract("SimpleStorage", function () { this.timeout(0); - before(function (done) { - const contractsConfig = { - contracts: { - "SimpleStorage": { - args: [100] - } - } - }; - embark.config(contractsConfig, () => { - done(); - }); - }); - it("should set constructor value", async function () { let result = await SimpleStorage.methods.storedData().call(); assert.strictEqual(parseInt(result, 10), 100); }); it("set storage value", async function () { - // TODO Solve from await SimpleStorage.methods.set(150).send(); let result = await SimpleStorage.methods.get().call(); assert.strictEqual(parseInt(result, 10), 499650); }); - });