What is the correct nesting order of await
and expect
for Chai matchers?
The following does not work:
await expect(contract.name()).to.equal("MyToken"); // (1) expect(await contract["transfer(address,uint256)"](wallet.address, 100)) .to.be.revertedWith('Insufficient funds'); // (2)
Errors thrown are respectively:
(1) AssertionError: expected Promise{…} to equal 'MyToken' (2) Uncaught RuntimeError: abort(TypeError: transactionCall is not a function).
The following works:
expect(await contract.name()).to.equal("MyToken"); // (1) await expect(contract["transfer(address,uint256)"](wallet.address, 100)) .to.be.revertedWith('Insufficient funds'); // (2)
So the problems seem to be:
(1) expect()
does not resolve a Promise
, and compares it directly to what is passed into .to.equal()
— this seems like an oversight!
(2) However expect()
does correctly catch errors, but only if they are thrown uncaught within the Promise
.