feat(@embark/test-runner): wait for deploy before enterning describe

This commit is contained in:
Jonathan Rainville 2019-06-13 12:54:52 -04:00
parent ae5b92ff45
commit 8c16541019
3 changed files with 27 additions and 3 deletions

View File

@ -27,8 +27,13 @@ config({
}); });
contract("AnotherStorage", function() { contract("AnotherStorage", function() {
const defaultAccount = accounts[0];
this.timeout(0); this.timeout(0);
it("should have got the default account in the describe", function () {
assert.strictEqual(defaultAccount, accounts[0]);
});
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

@ -208,6 +208,7 @@ class TestRunner {
let fns = files.map((file) => { let fns = files.map((file) => {
return (cb) => { return (cb) => {
const mocha = new Mocha(); const mocha = new Mocha();
mocha.delay();
const gasLimit = options.coverage ? COVERAGE_GAS_LIMIT : GAS_LIMIT; const gasLimit = options.coverage ? COVERAGE_GAS_LIMIT : GAS_LIMIT;
const reporter = options.inProcess ? EmbarkApiSpec : EmbarkSpec; const reporter = options.inProcess ? EmbarkApiSpec : EmbarkSpec;
mocha.reporter(reporter, { mocha.reporter(reporter, {
@ -219,17 +220,35 @@ class TestRunner {
mocha.addFile(file); mocha.addFile(file);
mocha.suite.timeout(0); mocha.suite.timeout(0);
mocha.suite.beforeAll('Wait for deploy', (done) => {
function describeWithWait(describeName, callback) {
if (global.embark.needConfig) { if (global.embark.needConfig) {
global.config({}); global.config({});
} }
global.embark.onReady(done); 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()`
});
});
}
mocha.suite.on('pre-require', function() {
// We do this to make such our globals don't get overriden by Mocha
global.describe = describeWithWait;
global.contract = describeWithWait;
}); });
// This populates Mocha to have describe(), etc.
mocha.suite.emit('pre-require', global, file, mocha);
mocha.run(function (fails) { mocha.run(function (fails) {
mocha.suite.removeAllListeners(); mocha.suite.removeAllListeners();
// Mocha prints the error already // Mocha prints the error already
cb(null, fails); cb(null, fails);
}); });
self.ogMochaDescribe = Mocha.describe;
}; };
}); });
async.series(fns, next); async.series(fns, next);

View File

@ -250,8 +250,8 @@ 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.events.emit('tests:ready');
callback(null, accounts); callback(null, accounts);
self.events.emit('tests:ready');
}); });
} }