Waffle icon indicating copy to clipboard operation
Waffle copied to clipboard

"Error: [object Promise] is not a valid transaction" when calling `expect().to.emit()`

Open MartinSchere opened this issue 3 years ago • 7 comments

Hi there!

I'm facing this error when trying to call the event catcher:

await expect(
  tokenFarm.depositTokens(tokens("100"), 0, {
    from: investor,
  })
)
  .to.emit(tokenFarm, "Deposit")
  .withArgs([investor, 0, tokens("97")]);

It seems to be caused by the following code:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitForPendingTransaction = void 0;
async function waitForPendingTransaction(tx, provider) {
    let hash;
    if (tx instanceof Promise) {
        ({ hash } = await tx); // Here the destructuring should be "tx" insdead of "hash"
    }
    else if (typeof tx === 'string') {
        hash = tx;
    }
    else {
        ({ hash } = tx);
    }
    if (!hash) {
        throw new Error(`${tx} is not a valid transaction`);
    }
    return provider.waitForTransaction(hash);
}
exports.waitForPendingTransaction = waitForPendingTransaction;

When destructuring hash as follows:

({tx:hash} = await tx)

The error is now TypeError: Cannot read property 'waitForTransaction' of undefined

I appreciate any help!

MartinSchere avatar Jun 24 '21 22:06 MartinSchere

I am seeing this same issue. I am just trying to figure out how to use await expect(...).to.emit()... but can't seem to get it to work. Have you made any progress debugging this in the last couple of months?

I get the same error (from your title):

Error: [object Promise] is not a valid transaction

From my code:

await expect(token0.transfer(bobby, 1000, { from: alice }))
    .to.emit(token0, 'Transfer')
    .withArgs(alice, bobby, 1000)

I am fairly new to Eth/Solidity development, so I am a bit lost here. Hope someone can help!

Thanks!

dbanks12 avatar Aug 25 '21 01:08 dbanks12

Have you guys had any luck finding a fix for this ?

stefanandy avatar Oct 13 '21 15:10 stefanandy

Any luck guys facing same issue.?

0xsatya avatar Dec 24 '21 18:12 0xsatya

Got the same error, using hardhat. Oddly, it only breaks on one of my 27 tests. It's the only test that calls a method from a foreign contract. I'll try to figure it out

greenlucid avatar Feb 08 '22 17:02 greenlucid

Okay, this is why I was having it: image You're supposed to be passing a contract as first param like this image

TypeScript saves my day. Not sure if that was your problem, though. Had to refactor everything to use TS but it was worth it

EDIT: I see that wasn't your issue. I remember another change I made, maybe this was the one that fixed it (I don't think so, though). Here, at the top, I had something like this. image If you leave it like this, without semicolons, the line below the getSigners is passed as parameter. You need to use a semicolon to enforce it as a separate statement. image I doubt any of this was the fix, but it's what I got. This is all magic to me

greenlucid avatar Feb 08 '22 22:02 greenlucid

I'm having the same problem... I've tried various combinations of awaiting, passing expect a function rather than a promise, etc. In the end, I have the same error (Error: [object Promise] is not a valid transaction).

Here is an excerpt of code triggering this error.

    it('Blah', async () => {
      const subject = access.paginateGovernors(0, 3);
      await expect(subject).to.emit(paginationLib, 'A');
    })

hickscorp avatar May 13 '22 11:05 hickscorp

Is anyone having this problem with Waffle 4? This part was worked on so the issue could already be fixed in Waffle 4.

rzadp avatar Aug 01 '22 14:08 rzadp