Skip to content

Improve error message when no await in chai matchers #7722

@kanej

Description

@kanej

A user lost time on their mocha test suite using chai matchers, as they where missing an await on an expect. The error shown was:

HardhatError: HHE100: An internal invariant was violated

We should try and detect for this case and show a clear error message suggesting that the likely cause is a lack of an await.

Example

it('Should transfer money to another wallet with extra value', async function () {
    const { accounts: { alice }, contracts: { drop }, others: { hashedElements, leaves, proofs, signature } } = await loadFixture(deployContractsFixture);
    const txn = await drop.claim(alice, 1, proofs[leaves.indexOf(hashedElements[0])], signature, { value: 10 });
    
    expect(txn).to.changeEtherBalance(ethers, alice, 10); // <-- missing await expect(txn)
});

Investigation

An initial look suggests something like the following is happening

  1. By missing out the await on expect(txn).to.changeEtherBalance(ethers, alice, 10);, the test completes and passes
  2. The test assertion is then run (in this case changeEtherBalance) because it was not awaited
  3. The assertion uses ethers underlying provider to make a getBlock request. The provider can't connect and ethers returns null
  4. The null block number trips the internal variant exception.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    Status

    In Review

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions