Skip to content

Commit

Permalink
fix: hardhat-chai-matchers error message when event is overloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
iosh authored and schaable committed Sep 9, 2024
1 parent 75d4219 commit 1a0e1e3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-moose-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nomicfoundation/hardhat-chai-matchers": patch
---

Fix hardhat-chai-matchers error message when event is overloaded
9 changes: 6 additions & 3 deletions packages/hardhat-chai-matchers/src/internal/emit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ export function supportEmit(

let eventFragment: EventFragment | null = null;
try {
eventFragment = contract.interface.getEvent(eventName);
} catch (e) {
// ignore error
eventFragment = contract.interface.getEvent(eventName, []);
} catch (e: unknown) {
if (e instanceof TypeError) {
const errorMessage = e.message.split(" (argument=")[0];
throw new AssertionError(errorMessage);
}
}

if (eventFragment === null) {
Expand Down
13 changes: 13 additions & 0 deletions packages/hardhat-chai-matchers/test/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,16 @@ export type EventsContract = BaseContract & {
};

export type AnotherContract = BaseContract & {};

export type OverrideEventContract = BaseContract & {
emitSimpleEventWithUintArg: BaseContractMethod<
[BigNumberish],
void,
ContractTransactionResponse
>;
emitSimpleEventWithoutArg: BaseContractMethod<
[],
void,
ContractTransactionResponse
>;
};
33 changes: 33 additions & 0 deletions packages/hardhat-chai-matchers/test/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
AnotherContract,
EventsContract,
MatchersContract,
OverrideEventContract,
} from "./contracts";

import { expect, AssertionError } from "chai";
Expand All @@ -14,6 +15,7 @@ import { useEnvironment, useEnvironmentWithNode } from "./helpers";
describe(".to.emit (contract events)", () => {
let contract: EventsContract;
let otherContract: AnotherContract;
let overrideEventContract: OverrideEventContract;
let matchers: MatchersContract;

describe("with the in-process hardhat network", function () {
Expand All @@ -38,6 +40,12 @@ describe(".to.emit (contract events)", () => {
)
).deploy(await otherContract.getAddress());

overrideEventContract = await (
await this.hre.ethers.getContractFactory<[], OverrideEventContract>(
"OverrideEventContract"
)
).deploy();

const Matchers = await this.hre.ethers.getContractFactory<
[],
MatchersContract
Expand Down Expand Up @@ -872,5 +880,30 @@ describe(".to.emit (contract events)", () => {
const tx = await contract.emitWithoutArgs();
await expect(tx.hash).to.emit(contract, "WithoutArgs");
});

describe("When event is overloaded", () => {
it("Should fail when the event name is ambiguous", async function () {
await expect(
expect(overrideEventContract.emitSimpleEventWithUintArg(1n)).to.emit(
overrideEventContract,
"simpleEvent"
)
).to.be.eventually.rejectedWith(
AssertionError,
`ambiguous event description (i.e. matches "simpleEvent(uint256)", "simpleEvent()")`
);
});

it("Should pass when the event name is not ambiguous", async function () {
await expect(overrideEventContract.emitSimpleEventWithUintArg(1n))
.to.emit(overrideEventContract, "simpleEvent(uint256)")
.withArgs(1);

await expect(overrideEventContract.emitSimpleEventWithoutArg()).to.emit(
overrideEventContract,
"simpleEvent()"
);
});
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ pragma solidity ^0.8.0;
contract Events {
AnotherContract anotherContract;

struct Struct { uint u; uint v; }
struct Struct {
uint u;
uint v;
}

event WithoutArgs();
event WithUintArg(uint u);
Expand Down Expand Up @@ -115,3 +118,16 @@ contract AnotherContract {
emit WithUintArg(u);
}
}

contract OverrideEventContract {
event simpleEvent(uint u);
event simpleEvent();

function emitSimpleEventWithUintArg(uint u) public {
emit simpleEvent(u);
}

function emitSimpleEventWithoutArg() public {
emit simpleEvent();
}
}

0 comments on commit 1a0e1e3

Please sign in to comment.