Unit Tests Fail Miserably

I am now in the process of thoroughly testing my token before deployment and followed your guides however some of my unit tests fail miserably and I am not sure why…

If anyone could have a quick look and direct me to a solution that would be greatly appreciated as I am still relatively new to all these new techs (Truffle and Co).

Console output:

  Initial State
    1) the deployer is the owner
    √ has a name (153ms)
    √ has a symbol (132ms)
    √ has 18 decimals (108ms)
    2) has a quadrilllion (10^15) initial total supply
    3) assigns the initial total supply to the owner

  Transfers
    4) not possible to send more tokens than an account has
    5) not possible to send tokens to the same account


  3 passing (3s)
  5 failing

  1) Initial State
       the deployer is the owner:

      AssertionError: expected '0xdA4c6fFC7Cb149a96b1D254def6544243259A33a' to equal '0x0De0c8f9dD61bC93039Eb4DE23f735fC41f80DF5'
      + expected - actual

      -0xdA4c6fFC7Cb149a96b1D254def6544243259A33a
      +0x0De0c8f9dD61bC93039Eb4DE23f735fC41f80DF5

      at Context.<anonymous> (test\ERC20Deflationary.test.js:19:41)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

  2) Initial State
       has a quadrilllion (10^15) initial total supply:
     Error: Assertion failed
      at assert (node_modules\bn.js\lib\bn.js:6:21)
      at BN._initNumber (node_modules\bn.js\lib\bn.js:128:7)
      at BN.init [as _init] (node_modules\bn.js\lib\bn.js:82:19)
      at new BN (node_modules\bn.js\lib\bn.js:39:12)
      at Context.<anonymous> (test\ERC20Deflationary.test.js:38:7)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

  3) Initial State
       assigns the initial total supply to the owner:

      AssertionError: expected '0' to equal '1000000000000000000000000000000000'
      + expected - actual

      -0
      +1000000000000000000000000000000000

      at Context.<anonymous> (test\ERC20Deflationary.test.js:47:42)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

  4) Transfers
       not possible to send more tokens than an account has:
     TypeError: Cannot read property 'balanceOf' of undefined
      at Context.<anonymous> (test\ERC20Deflationary.test.js:61:47)
      at processImmediate (internal/timers.js:461:21)

  5) Transfers
       not possible to send tokens to the same account:
     TypeError: Cannot read property 'balanceOf' of undefined
      at Context.<anonymous> (test\ERC20Deflationary.test.js:76:47)
      at processImmediate (internal/timers.js:461:21)

Thanks in advance,
aress31

This is not deploying the contract. Use ERC20Deflationary.new({ from: owner }).

@frangio if I use this format then I need to initialise all the parameters of my constructor which are already initialised in my 2_deploy.js.

Then the contract is being deployed with a different owner. from: owner in your tests is ignored.

It sounds like you’re using Truffle migrations together with OpenZeppelin Test Environment.

This combination is not supported. You should be using truffle test, and remove the line where you get the list of accounts from test-environment. For how to obtain the list of accounts in Truffle tests check out their docs.

1 Like

Thanks @frangio I kind of switch to full Truffle testing and the owner test now works, however, the it("has a quadrilllion (10^15) initial total supply", async () => { and it("assigns the initial total supply to the owner", async () => { are still failing, would you mind taking a quick look at the following and guide me to the solutiton?

This is the stacktrace:

  Contract: Initial State
    √ the deployer is the owner (115ms)
    √ has a name (125ms)
    √ has a symbol (141ms)
    √ has 18 decimals (108ms)
    1) has a quadrilllion (10^15) initial total supply
    > No events were emitted
    2) assigns the initial total supply to the owner
    > No events were emitted

  Contract: Transfers
    3) not possible to send zero tokens
    > No events were emitted
Error: Returned error: VM Exception while processing transaction: revert Amount cannot be null -- Reason given: Amount cannot be null.
    4) "before each" hook: before test for "not possible to send more tokens than an account has"
    5) "before each" hook: before test for "not possible to send more tokens than an account has"


  4 passing (4s)
  5 failing

  1) Contract: Initial State
       has a quadrilllion (10^15) initial total supply:
     TypeError: Cannot set property 'negative' of undefined
      at BN (node_modules\bn.js\lib\bn.js:26:19)
      at Context.<anonymous> (test\ERC20Deflationary.test.js:35:17)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

  2) Contract: Initial State
       assigns the initial total supply to the owner:
     TypeError: Cannot read property 'logs' of null
      at inTransaction (node_modules\@openzeppelin\test-helpers\src\expectEvent.js:96:35)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
      at Context.<anonymous> (test\ERC20Deflationary.test.js:48:5)

  3) Contract: Transfers
       not possible to send zero tokens:
     Error: Invalid Chai property: rejected
      at Object.proxyGetter [as get] (node_modules\chai\lib\chai\utils\proxify.js:78:17)
      at Context.<anonymous> (test\ERC20Deflationary.test.js:64:12)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

  4) Contract: Transfers
       "before each" hook: before test for "not possible to send more tokens than an account has":
     Uncaught RuntimeError: abort(Error: Returned error: VM Exception while processing transaction: revert Amount cannot be null -- Reason given: Amount cannot be null.). Build with -s ASSERTIONS=1 for more info.
      at process.abort (C:\Users\ateyar\.config\truffle\compilers\node_modules\soljson-v0.8.4+commit.c7e474f2.js:1:13012)
      at process.emit (C:\Users\ateyar\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\source-map-support\source-map-support.js:495:1)   
      at processPromiseRejections (internal/process/promises.js:245:33)
      at processTicksAndRejections (internal/process/task_queues.js:94:32)

  5) Contract: Transfers
       "before each" hook: before test for "not possible to send more tokens than an account has":
     Error: done() called multiple times in hook <Contract: Transfers "before each" hook: before test for "not possible to send more tokens than an account has"> of file C:\Users\ateyar\Documents\GitHub\erc20-deflationary\test\ERC20Deflationary.test.js
      at processTicksAndRejections (internal/process/task_queues.js:93:5)

It seems that the issues have to do with BigNumber and expectEvent after further troubleshooting I am getting this stacktrace:

  Contract: Initial State
    √ the deployer is the owner (128ms)
    √ has a name (131ms)
    √ has a symbol (172ms)
    √ has 18 decimals (111ms)
BN {
  negative: 0,
  words: [ 0, 5685888, 14234508, 20388114, 49, <1 empty item> ],
  length: 5,
  red: null
}
BN { negative: 0, words: [ 23532 ], length: 1, red: null }
    √ has a quadrilllion (10^15) initial total supply (222ms)
    1) assigns the initial total supply to the owner
    > No events were emitted


  5 passing (3s)
  1 failing

  1) Contract: Initial State
       assigns the initial total supply to the owner:
     Error: Returned error: Returned error: invalid argument 0: json: cannot unmarshal non-string into Go value of type common.Hash
      at Object.ErrorResponse (node_modules\web3-core-helpers\lib\errors.js:28:19)
      at C:\Users\ateyar\Documents\GitHub\erc20-deflationary\node_modules\web3-core-requestmanager\lib\index.js:303:36
      at C:\Users\ateyar\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\provider\wrapper.js:107:1
      at XMLHttpRequest.request.onreadystatechange (C:\Users\ateyar\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\web3\node_modules\web3-providers-http\lib\index.js:98:1)
      at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\ateyar\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:1)
      at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._setReadyState (C:\Users\ateyar\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:208:1)
      at XMLHttpRequest.exports.modules.996763.XMLHttpRequest._onHttpResponseEnd (C:\Users\ateyar\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:318:1)
      at IncomingMessage.<anonymous> (C:\Users\ateyar\AppData\Roaming\npm\node_modules\truffle\build\webpack:\node_modules\xhr2-cookies\dist\xml-http-request.js:289:47)
      at endReadableNT (internal/streams/readable.js:1327:12)
      at processTicksAndRejections (internal/process/task_queues.js:80:21)

for the following tests:

it("has a quadrilllion (10^15) initial total supply", async () => {
    const decimals = await this.token.decimals();
    const totalSupply = await this.token.totalSupply();
    console.log(totalSupply);
    console.log(new BN((10 ** 32).toString()));
    // console.log((10 ** 15 * 10 ** decimals.toNumber()).toString());
    // console.log(totalSupply, expected);
    // expect(await this.token.totalSupply()).to.be.bignumber.equal(
    //   (10 ** 15 * 10 ** decimals.toNumber()).toString()
    // );
  });

  it("assigns the initial total supply to the owner", async () => {
    const [owner] = accounts;
    const totalSupply = await this.token.totalSupply();
    const ownerBalance = await this.token.balanceOf(owner);
    expect(ownerBalance).to.be.bignumber.equal(totalSupply);
    // TODO: Troubleshot this
    await expectEvent.inConstruction(this.token, "Transfer", {
      from: ZERO_ADDRESS,
      to: owner,
      value: totalSupply,
    });

Edit: Opened a ticket https://github.com/indutny/bn.js/issues/274