1

Problem: facing an issue with the below Truffle test case while trying to Implement the ERC20 token in my contract.

contract("Token Test", async(accounts) => {

const[deployerAccount, recipient, anotherAccount] = accounts;

it("All tokens should be in deployer's account", async() => {
    let instance = await Token.deployed();
    let totalSupply = await instance.totalSupply();
    // let balance = await instance.balanceOf(accounts[0]);
    // assert.equal(balance.valueOf(), totalSupply.valueOf(), "The Balance Was not same");

    expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
});

it("Is possible to send tokens between accounts", async() => {
    const sendToken = 1;
    let instance = await Token.deployed();
    let totalSupply = await instance.totalSupply();
    expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);
    expect(instance.transfer(recipient, sendToken)).to.eventually.be.fulfilled;
    expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply.sub(new BN(sendToken)));
    expect(instance.balanceOf(recipient)).to.eventually.be.a.bignumber.equal(new BN(sendToken));
}); 

it("Should not be able to send more token than available with owner", async() => {
    let instance = await Token.deployed();
    let balanceOfDeployer = await instance.balanceOf(deployerAccount);
    console.log(balanceOfDeployer+ " : " + await instance.balanceOf(deployerAccount));
    // expect(instance.transfer(recipient, new BN(balanceOfDeployer+3))).to.eventually.be.rejected;

    expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(balanceOfDeployer);
});

});

Description: When I am trying to execute test cases 2 and 3 at the same time it is failing with the below error:

"before all" hook: prepare suite for "Should not be able to send more token than available with owner":

  Uncaught AssertionError: expected '1000000000' to equal '999999999'
  + expected - actual

  -1000000000
  +999999999
  
  at /project_dir/node_modules/chai-as-promised/lib/chai-as-promised.js:302:22
  at processTicksAndRejections (internal/process/task_queues.js:95:5)



UnhandledRejections detected
Promise {
  <rejected> AssertionError: expected '1000000000' to equal '999999999'
      at /project_dir/node_modules/chai-as-promised/lib/chai-as-promised.js:302:22
      at processTicksAndRejections (internal/process/task_queues.js:95:5) {
    showDiff: true,
    actual: '1000000000',
    expected: '999999999',
    operator: 'strictEqual',
    uncaught: true
  }

However when I try to execute both test cases independently (commenting test case 2 keeping 3, and vice-versa). They work fine and are completed without any error.

Need help understanding what am I missing here and how to fix it.

Things I have tried: Initially, I thought that this is might be happening due to state change in the variable in test case 2. So I moved test case 3 to a new contract(). But I was still facing the same issue. However, this should not be happening as contract() provides a clean room environment before executing every contract test.

P.S.: I am initiating the value of totalSupply to 1000000000 in the constructor of Contract.

2 Answers 2

1
+50

Your .to. should be replaced with .should. to support the .eventually. interface.

Explained in Chai docs, you

The most powerful extension provided by Chai as Promised is the eventually property. With it, you can transform any existing Chai assertion into one that acts on a promise:

return promise.should.be.fulfilled;
return promise.should.eventually.deep.equal("foo");
return promise.should.become("foo"); // same as `.eventually.deep.equal`
return promise.should.be.rejected;
return promise.should.be.rejectedWith(Error); // other variants of Chai's `throw` assertion work too.

But you don't need to use chai-as-promised because the comparison should be a promise too, even if you change to to should you'll still get an error TypeError: <BN: 3b9aca00> is not a thenable.

So simple drop .eventually and add await on the expect param, so this

expect(instance.balanceOf(deployerAccount)).should.eventually.be.a.bignumber.equal(totalSupply);

will transform to

expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(totalSupply);

and it will work properly.

Full working tests -

    it("All tokens should be in deployer's account", async () => {
        let instance = await Token.deployed();
        let totalSupply = await instance.totalSupply();
        // let balance = await instance.balanceOf(accounts[0]);
        // assert.equal(balance.valueOf(), totalSupply.valueOf(), "The Balance Was not same");

        expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(totalSupply);
    });

    it("Is possible to send tokens between accounts", async () => {
        const sendToken = 1;
        let instance = await Token.deployed();
        let totalSupply = await instance.totalSupply();
        expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(totalSupply);
        await instance.transfer(recipient, sendToken);
        expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(totalSupply.sub(new BN(sendToken)));
        expect(await instance.balanceOf(recipient)).to.be.a.bignumber.equal(new BN(sendToken));
    });

    it("Should not be able to send more token than available with owner", async () => {
        let instance = await Token.deployed();
        let balanceOfDeployer = await instance.balanceOf(deployerAccount);
        console.log(balanceOfDeployer + " : " + await instance.balanceOf(deployerAccount));

        expect(await instance.balanceOf(deployerAccount)).to.be.a.bignumber.equal(balanceOfDeployer);
    });
0

I think this might be a concurrency issue. Have you tried placing await before each and every possibly async call? Example:

await expect(instance.balanceOf(deployerAccount)).to.eventually.be.a.bignumber.equal(totalSupply);

Also, I would try replacing:

expect(instance.transfer(recipient, sendToken)).to.eventually.be.fulfilled;

with:

await instance.transfer(recipient, sendToken);

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.