PatrickAlphaC / hardhat-nft-marketplace-fcc

114 stars 97 forks source link

Unit tests: Waiting for events -> wrong syntax #23

Open jan10101 opened 2 years ago

jan10101 commented 2 years ago

I noticed an issue in the unit tests. The following syntax is used:

expect(await nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE)).to.emit(
                      "ItemListed"
                  )

This seems to be incorrect. I can type anything for the event name and the test will pass. E.g.:

expect(await nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE)).to.emit(
                      "anything?"
                  )

Reading the chai docs it seems the "await" needs to be in front of the "expect" like this:

await expect(nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE)).to.emit(
                      "ItemListed"
                  )

But doing so fails with the following error message:

 TypeError: Cannot read properties of undefined (reading 'waitForTransaction')
      at Object.waitForPendingTransaction (node_modules/@ethereum-waffle/chai/dist/cjs/matchers/misc/transaction.js:18:21)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at runNextTicks (node:internal/process/task_queues:65:3)
      at listOnTimeout (node:internal/timers:528:9)
      at processTimers (node:internal/timers:502:7)
      at Context.<anonymous> (test/unit/NftMarketplace.test.js:27:19)

Does anyone know why this is failing and how we could get a working code for listening for events?

alle-bartoli commented 1 year ago

Hi Jan 👋

emit needs two argument. From hardhat-chai-matchers source code:

emit(contract: any, eventName: string): EmitAssertion;

So, try first to pass it all, as follow:

it("emits an event after listing an item", async () => {
    await expect(nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE)).to.emit(
        nftMarketplace,
        "ItemListed"
    )
})

Cheers

penrychou commented 12 months ago

hi, I got the same error image help!!!!

BeamNawapat commented 7 months ago

hi, I got the same error image help!!!!

Did you already import @nomicfoundation/hardhat-chai-matchers in hardhat config?