From 332229ff9d8a7cc2d4b9ea1fdaa7776f04ea5532 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 13 Jun 2019 13:37:11 -0400 Subject: [PATCH] feat(@embark/test-runner): return accounts in the describe callback --- dapps/tests/app/test/another_storage_spec.js | 6 +++++- packages/embark-test-runner/src/index.js | 10 +++------- packages/embark-test-runner/src/test.js | 16 +++++++++++----- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/dapps/tests/app/test/another_storage_spec.js b/dapps/tests/app/test/another_storage_spec.js index 143e6ef92..b77632b01 100644 --- a/dapps/tests/app/test/another_storage_spec.js +++ b/dapps/tests/app/test/another_storage_spec.js @@ -26,7 +26,7 @@ config({ accounts = theAccounts; }); -contract("AnotherStorage", function() { +contract("AnotherStorage", function(accountsAgain) { const defaultAccount = accounts[0]; this.timeout(0); @@ -34,6 +34,10 @@ contract("AnotherStorage", function() { assert.strictEqual(defaultAccount, accounts[0]); }); + it("should have the accounts in the describe callback too", function () { + assert.deepStrictEqual(accountsAgain, accounts); + }); + it("should have account with balance", async function() { let balance = await web3.eth.getBalance(accounts[0]); assert.ok(parseInt(balance, 10) > 4900000000000000000); diff --git a/packages/embark-test-runner/src/index.js b/packages/embark-test-runner/src/index.js index 49f88ba84..9c403f945 100644 --- a/packages/embark-test-runner/src/index.js +++ b/packages/embark-test-runner/src/index.js @@ -225,13 +225,9 @@ class TestRunner { if (global.embark.needConfig) { global.config({}); } - global.embark.onReady(() => { - // Next tick makes sure to not have a hang when tests don't use `config()` - // I think this is needed because Mocha expects global.run() to be called ina further event loop - process.nextTick(() => { - self.ogMochaDescribe(describeName, callback); - global.run(); // This tells mocha that it can run the test (used in conjunction with `delay()` - }); + global.embark.onReady((_err, accounts) => { + self.ogMochaDescribe(describeName, callback.bind(mocha, accounts)); + global.run(); // This tells mocha that it can run the test (used in conjunction with `delay()` }); } diff --git a/packages/embark-test-runner/src/test.js b/packages/embark-test-runner/src/test.js index a47a15408..dde1e64f1 100644 --- a/packages/embark-test-runner/src/test.js +++ b/packages/embark-test-runner/src/test.js @@ -26,6 +26,7 @@ class Test { this.accounts = []; this.embarkjs = {}; this.dappPath = options.dappPath; + this.accounts = []; this.events.setCommandHandler("blockchain:provider:contract:accounts:get", cb => { this.events.request("blockchain:getAccounts", cb); @@ -112,10 +113,14 @@ class Test { onReady(callback) { const self = this; if (this.ready) { - return callback(); + return setImmediate(() => { + callback(null, this.accounts); + }); } if (this.error) { - return callback(this.error); + return setImmediate(() => { + callback(this.error); + }); } let errorCallback, readyCallback; @@ -125,9 +130,9 @@ class Test { callback(err); }; - readyCallback = () => { + readyCallback = (accounts) => { self.events.removeListener('tests:deployError', errorCallback); - callback(); + callback(null, accounts); }; this.events.once('tests:ready', readyCallback); @@ -250,8 +255,9 @@ class Test { // TODO Do not exit in case of not a normal run (eg after a change) if (!self.options.inProcess) process.exit(1); } + self.accounts = accounts; callback(null, accounts); - self.events.emit('tests:ready'); + self.events.emit('tests:ready', accounts); }); }