Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

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

Open
jan10101 opened this issue Nov 19, 2022 · 3 comments
Open

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

jan10101 opened this issue Nov 19, 2022 · 3 comments

Comments

@jan10101
Copy link

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
Copy link

alle-bartoli commented Dec 11, 2022

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
Copy link

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

@BeamNawapat
Copy link

BeamNawapat commented Apr 4, 2024

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

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants