Skip to content

Commit e584429

Browse files
authored
Merge pull request #3832 from NomicFoundation/chai-matchers-with-args-array-length
Fix .withArgs when arrays have different length
2 parents 3fba4cf + 06c4797 commit e584429

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

.changeset/lazy-hornets-clap.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@nomicfoundation/hardhat-chai-matchers": patch
3+
---
4+
5+
Fixed a problem when `.withArgs` was used with arrays with different length

packages/hardhat-chai-matchers/src/internal/emit.ts

+11
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,17 @@ function assertArgsArraysEqual(
165165
expectedArgs[index]?.length !== undefined &&
166166
typeof expectedArgs[index] !== "string"
167167
) {
168+
const expectedLength = expectedArgs[index].length;
169+
const actualLength = actualArgs[index].length;
170+
assert(
171+
expectedLength === actualLength,
172+
`Expected the ${ordinal(
173+
index + 1
174+
)} argument of the "${eventName}" event to have ${expectedLength} ${
175+
expectedLength === 1 ? "element" : "elements"
176+
}, but it has ${actualLength}`
177+
);
178+
168179
for (let j = 0; j < expectedArgs[index].length; j++) {
169180
new Assertion(actualArgs[index][j], undefined, ssfi, true).equal(
170181
expectedArgs[index][j]

packages/hardhat-chai-matchers/src/internal/reverted/revertedWithCustomError.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AssertionError } from "chai";
2+
import ordinal from "ordinal";
23

34
import { buildAssert, Ssfi } from "../../utils";
45
import { decodeReturnData, getReturnDataFromError } from "./utils";
@@ -193,6 +194,16 @@ export async function revertedWithCustomErrorWithArgs(
193194
throw e;
194195
}
195196
} else if (Array.isArray(expectedArg)) {
197+
const expectedLength = expectedArg.length;
198+
const actualLength = actualArg.length;
199+
assert(
200+
expectedLength === actualLength,
201+
`Expected the ${ordinal(i + 1)} argument of the "${
202+
customError.name
203+
}" custom error to have ${expectedLength} ${
204+
expectedLength === 1 ? "element" : "elements"
205+
}, but it has ${actualLength}`
206+
);
196207
new Assertion(actualArg).to.deep.equal(expectedArg);
197208
} else {
198209
new Assertion(actualArg).to.equal(expectedArg);

packages/hardhat-chai-matchers/test/events.ts

+20
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,26 @@ describe(".to.emit (contract events)", () => {
337337
"expected 1 to equal 3"
338338
);
339339
});
340+
341+
it("Should fail when the arrays don't have the same length", async function () {
342+
await expect(
343+
expect(contract.emitUintArray(1, 2))
344+
.to.emit(contract, "WithUintArray")
345+
.withArgs([1])
346+
).to.be.eventually.rejectedWith(
347+
AssertionError,
348+
'Expected the 1st argument of the "WithUintArray" event to have 1 element, but it has 2'
349+
);
350+
351+
await expect(
352+
expect(contract.emitUintArray(1, 2))
353+
.to.emit(contract, "WithUintArray")
354+
.withArgs([1, 2, 3])
355+
).to.be.eventually.rejectedWith(
356+
AssertionError,
357+
'Expected the 1st argument of the "WithUintArray" event to have 3 elements, but it has 2'
358+
);
359+
});
340360
});
341361

342362
describe("with a bytes32 array argument", function () {

packages/hardhat-chai-matchers/test/reverted/revertedWithCustomError.ts

+20
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,26 @@ describe("INTEGRATION: Reverted with custom error", function () {
317317
);
318318
});
319319

320+
it("should fail when the arrays don't have the same length", async function () {
321+
await expect(
322+
expect(matchers.revertWithCustomErrorWithPair(1, 2))
323+
.to.be.revertedWithCustomError(matchers, "CustomErrorWithPair")
324+
.withArgs([1])
325+
).to.be.rejectedWith(
326+
AssertionError,
327+
'Expected the 1st argument of the "CustomErrorWithPair" custom error to have 1 element, but it has 2'
328+
);
329+
330+
await expect(
331+
expect(matchers.revertWithCustomErrorWithPair(1, 2))
332+
.to.be.revertedWithCustomError(matchers, "CustomErrorWithPair")
333+
.withArgs([1, 2, 3])
334+
).to.be.rejectedWith(
335+
AssertionError,
336+
'Expected the 1st argument of the "CustomErrorWithPair" custom error to have 3 elements, but it has 2'
337+
);
338+
});
339+
320340
it("Should fail when used with .not.", async function () {
321341
expect(() =>
322342
expect(matchers.revertWithSomeCustomError())

0 commit comments

Comments
 (0)