-
What difference make where we put await, I can guess that we are waiting for different promises to resolve but don't get it fully.. await expect( nftMarketplace.cancelListing(basicNft.address, TOKEN_ID)).to.be.revertedWith("NotOwner")}) expect(await nftMarketplace.cancelListing(basicNft.address, TOKEN_ID)).to.emit("ItemCanceled") |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
This one is slightly incorrect: expect(await nftMarketplace.cancelListing(basicNft.address, TOKEN_ID)).to.emit("ItemCanceled") This is the apt one: await expect( nftMarketplace.cancelListing(basicNft.address,TOKEN_ID)).to.be.revertedWith("NotOwner")}) Why it's incorrect? Because it is plausible for another statement to be executed before the await keyword gets triggered in the first place. And to remove that possibility it is best advised to follow using the Of course, you can use double awaits here if cancelListing is something which could be inconsistent, but I would not recommend following solely the second method (await inside), it is best paired to avoid the above possibility. |
Beta Was this translation helpful? Give feedback.
-
expect(await nftMarketplace.cancelListing(basicNft.address, TOKEN_ID)).to.emit("ItemCanceled") Above statement is equivalent to const tx = await nftMarketplace.cancelListing(basicNft.address, TOKEN_ID);
expect(tx).to.emit("ItemCanceled"); because it first wait for transaction to finish, then expect whatever with the result of finished transaction. await expect( nftMarketplace.cancelListing(basicNft.address,TOKEN_ID)).to.be.revertedWith("NotOwner")); is not waiting for transaction to finish. It is expecting during transaction execution (transaction can only revert during execution. i.e if a transaction is finished, then it means the transaction was not reverted) const tx = nftMarketplace.cancelListing(basicNft.address,TOKEN_ID);
// Note we're not waiting for transaction to finish
await expect(tx).to.be.revertedWith("NotOwner");
|
Beta Was this translation helpful? Give feedback.
-
Note that the Hardhat docs. |
Beta Was this translation helpful? Give feedback.
This one is slightly incorrect:
This is the apt one:
Why it's incorrect? Because it is plausible for another statement to be executed before the await keyword gets triggered in the first place. And to remove that possibility it is best advised to follow using the
await
keyword before the statement.Of course, you can use double awaits here if cancelListing is something which could be inconsistent, but I would not recommend following solely the second method (await inside), it is b…