feat(@embark/test-runner): return accounts in the describe callback

This commit is contained in:
Jonathan Rainville 2019-06-13 13:37:11 -04:00
parent 8c16541019
commit 332229ff9d
3 changed files with 19 additions and 13 deletions

View File

@ -26,7 +26,7 @@ config({
accounts = theAccounts; accounts = theAccounts;
}); });
contract("AnotherStorage", function() { contract("AnotherStorage", function(accountsAgain) {
const defaultAccount = accounts[0]; const defaultAccount = accounts[0];
this.timeout(0); this.timeout(0);
@ -34,6 +34,10 @@ contract("AnotherStorage", function() {
assert.strictEqual(defaultAccount, accounts[0]); 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() { it("should have account with balance", async function() {
let balance = await web3.eth.getBalance(accounts[0]); let balance = await web3.eth.getBalance(accounts[0]);
assert.ok(parseInt(balance, 10) > 4900000000000000000); assert.ok(parseInt(balance, 10) > 4900000000000000000);

View File

@ -225,14 +225,10 @@ class TestRunner {
if (global.embark.needConfig) { if (global.embark.needConfig) {
global.config({}); global.config({});
} }
global.embark.onReady(() => { global.embark.onReady((_err, accounts) => {
// Next tick makes sure to not have a hang when tests don't use `config()` self.ogMochaDescribe(describeName, callback.bind(mocha, accounts));
// 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.run(); // This tells mocha that it can run the test (used in conjunction with `delay()`
}); });
});
} }
mocha.suite.on('pre-require', function() { mocha.suite.on('pre-require', function() {

View File

@ -26,6 +26,7 @@ class Test {
this.accounts = []; this.accounts = [];
this.embarkjs = {}; this.embarkjs = {};
this.dappPath = options.dappPath; this.dappPath = options.dappPath;
this.accounts = [];
this.events.setCommandHandler("blockchain:provider:contract:accounts:get", cb => { this.events.setCommandHandler("blockchain:provider:contract:accounts:get", cb => {
this.events.request("blockchain:getAccounts", cb); this.events.request("blockchain:getAccounts", cb);
@ -112,10 +113,14 @@ class Test {
onReady(callback) { onReady(callback) {
const self = this; const self = this;
if (this.ready) { if (this.ready) {
return callback(); return setImmediate(() => {
callback(null, this.accounts);
});
} }
if (this.error) { if (this.error) {
return callback(this.error); return setImmediate(() => {
callback(this.error);
});
} }
let errorCallback, readyCallback; let errorCallback, readyCallback;
@ -125,9 +130,9 @@ class Test {
callback(err); callback(err);
}; };
readyCallback = () => { readyCallback = (accounts) => {
self.events.removeListener('tests:deployError', errorCallback); self.events.removeListener('tests:deployError', errorCallback);
callback(); callback(null, accounts);
}; };
this.events.once('tests:ready', readyCallback); 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) // TODO Do not exit in case of not a normal run (eg after a change)
if (!self.options.inProcess) process.exit(1); if (!self.options.inProcess) process.exit(1);
} }
self.accounts = accounts;
callback(null, accounts); callback(null, accounts);
self.events.emit('tests:ready'); self.events.emit('tests:ready', accounts);
}); });
} }