1

Awaiting the resolution of calls to a node works for me, like this example:

describe('some contract', async function () {

    ...

    it('data should be written correctly', async function () {
        let expectedResult = 1;
        let actualResult = await instance.getSomeData(param1);
        assert(actualResult == expectedResult); 
    })

What gives me weird behaviour - a timeout to be specific, is when I try to assert inside the callback of a promise like this:

describe('some contract', async function () {

    ...

    it('data should be written correctly', async function () {
        let expectedResult = 1;
        instance.getSomeData(param1).then(result => {
            assert(result == expectedResult); 
        })
    })

Is there any way to do this, or is the only way to await the result and then assert?

3
2

It's rather a JS question, but anyway, here the answer, since it's useful to know when writing Ethereum tests in JS:

The problem with your second code fragment is that your test case is considered as done before your "then"-part is executed. I would recommend to try not to mix async/await and then as far as possible.

Variant 1 (no async function, return the promise):

it('data should be written correctly', function () {
    let expectedResult = 1;

    return instance.getSomeData(param1).then(result => {
        assert(result == expectedResult);
    })
})

Variant 2 (async function with await):

it('data should be written correctly', async function () {
    let expectedResult = 1;

    var result = await instance.getSomeData(param1);
    assert(result == expectedResult);
})

Moreover, I am not quite sure if this is a good idea:

describe('some contract', async function () {...})

There is no reason to have an async function here when you only have it-calls inside?

2
  • You're right that adding the async to a describe call is definitely a bad idea. It can cause tests to fail in a seemingly "random" way. It should definitely just be describe('some contract', function () {...}). Jan 30 '19 at 10:47
  • That helped (also your note about the extra async in describe) - only thing missing is that mocha seems not to exit on itself, only solution is to call mocha --exit
    – Alex
    Jan 31 '19 at 8:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.