1

I'm making test script for my contract and it looks likes

contract('...', async () => {
    let token = null;
    before('deploy', async () => {
        token = await MyContract.new(...);
    });

    it('should success', async() => { ... });
    it('should fail', async() => { ... });    // work as intended only until here
    it('should success', async() => { ... });
});

and executed with command truffle test ./test/test.js

Issue is that calls after second it(...), which contract call fails and invoke revert, never succeed and raise errors similar to

AssertionError: the tx doesn't have the correct nonce. account has nonce of: 37 tx has nonce of: 36

How do I make the test script work? looks like some kind of nonce increment gone wrong, but I'm not sure what I've done wrong. I'm using Ganache GUI 2.0.1 for test network and truffle v5.0.9.

-- Edit --

as comment suggested, I check if I missed any await keyword,

my code is all like this,

it('should deny transfer with insufficient (zero) balance', async () => {
    const amount = web3.utils.toWei('1', 'ether');
    const promise = token.transfer.call(accounts[3], amount, { 'from': accounts[2] });
    await assert.isRejected(promise, 'revert', 'transfer transaction with insufficient (zero) balance must be denied');
});

I tried what Majd TL suggested also,

it('should deny transfer with insufficient (zero) balance', async () => {
    const amount = web3.utils.toWei('1', 'ether');
    const promise = token.transfer(accounts[3], amount, { 'from': accounts[2] });
    await truffleAssert.reverts(promise, 'revert', 'transfer transaction with insufficient (zero) balance must be denied');
});

but it does not resolve the problem. The workaround now is to call contract call that expected to fail with .call, like

const promise = token.nope.call(1, 2, 3, { 'from': accounts[0] });

but in case of really unexpected error, it is inevitable and should be a thing. I Also see this error when using multiple contract(...).

  • 1
    I bet you're missing an await somewhere... – goodvibration May 9 at 8:42
  • what is amount and why it is too big?, also i'm saddly not javascript profi :) but maybe making the whole test in one line could change something, i mean -> await truffleAssert.reverts(token.transfer(accounts[3], 2, .......) – Majd TL May 13 at 7:56
  • I think it's not caused by promise, because I managed to make it work by managing nonce per user by hand. So my guess is truffle does not handle nonce when there's errors and manual contract deployment has occurred. and, well I checked every promise I tossed : ( – xvnm May 13 at 8:23
  • what does the amount value stand for? – Majd TL May 13 at 8:52
  • amount value stands for 1 ether, (== 10**18). It seems to big but bignumber type can store and handle it. Also I thought it's not a uncommon to use this large value in token contract. – xvnm May 13 at 8:55
1

Use the truffle assertions library and as goodvibration said don't forget the await

const truffleAssert = require('truffle-assertions');    
...
it('should fail', async function () {
   await truffleAssert.fails(<your_contract>.<your_function>(..., { from: ... }));
});

https://www.npmjs.com/package/truffle-assertions

  • thanks for reply, but it could not solve the problem. I replaced my code with your suggestion and check every line if I missed await. – xvnm May 13 at 0:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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