Skip to content

Commit 58643e8

Browse files
committed
Fix events tests, round 2
1 parent 4748164 commit 58643e8

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function assertArgsArraysEqual(
6262
ssfi
6363
);
6464
} catch (err: any) {
65-
// throw new AssertionError('Fix this');
65+
// throw new AssertionError("Fix this");
6666
err.message = `Error in ${tag}: ${err.message}`;
6767
throw err;
6868
}

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

+35-45
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ describe(".to.emit (contract events)", () => {
170170
.withArgs(otherAddressable)
171171
).to.be.eventually.rejectedWith(
172172
AssertionError,
173-
`expected '${address}' to equal '${otherAddress}'`
173+
`Error in "WithAddressArg" event: Error in the 1st argument assertion: expected '${address}' to equal '${otherAddress}'`
174174
);
175175
});
176176

@@ -181,7 +181,7 @@ describe(".to.emit (contract events)", () => {
181181
.withArgs(otherAddress)
182182
).to.be.eventually.rejectedWith(
183183
AssertionError,
184-
`expected '${address}' to equal '${otherAddress}'`
184+
`Error in "WithAddressArg" event: Error in the 1st argument assertion: expected '${address}' to equal '${otherAddress}'`
185185
);
186186
});
187187

@@ -212,11 +212,13 @@ describe(".to.emit (contract events)", () => {
212212
}
213213

214214
function formatBytes(str: string) {
215-
const strBytes = ethers.hexlify(ethers.toUtf8Bytes(str));
215+
const bytes = ethers.hexlify(ethers.toUtf8Bytes(str));
216+
const bytes32 = ethers.zeroPadValue(bytes, 32);
216217
return {
217218
...formatHash(str),
218-
bytes: strBytes,
219-
bytes32: ethers.zeroPadValue(strBytes, 32),
219+
bytes,
220+
bytes32,
221+
abbrev32: abbrev(ethers.hexlify(bytes32)),
220222
};
221223
}
222224

@@ -267,7 +269,7 @@ describe(".to.emit (contract events)", () => {
267269
.withArgs(str1.hash)
268270
).to.be.eventually.rejectedWith(
269271
AssertionError,
270-
"The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion should be the actual event argument (the pre-image of the hash). You provided the hash itself. Please supply the actual event argument (the pre-image of the hash) instead."
272+
"The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion should be the actual event argument (the pre-image of the hash). You provided the hash itself. Please supply the actual event argument (the pre-image of the hash) instead"
271273
);
272274
});
273275

@@ -279,7 +281,7 @@ describe(".to.emit (contract events)", () => {
279281
.withArgs(incorrect.str)
280282
).to.be.eventually.rejectedWith(
281283
AssertionError,
282-
`Error in "WithIndexedStringArg" event: Error in the 1st argument assertion: The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion was hashed to produce ${incorrect.hash}. The actual hash and the expected hash ${str1.hash} did not match: expected '${str1.abbrev}' to equal '${incorrect.abbrev}'`
284+
`Error in "WithIndexedStringArg" event: Error in the 1st argument assertion: The actual value was an indexed and hashed value of the event argument. The expected value provided to the assertion was hashed to produce ${incorrect.hash}. The actual hash and the expected hash ${str1.hash} did not match: expected '${str1.abbrev}' to equal '${incorrect.abbrev}`
283285
);
284286
});
285287
});
@@ -321,7 +323,7 @@ describe(".to.emit (contract events)", () => {
321323
);
322324
});
323325

324-
it("Should match the event argument with a hash value", async function () {
326+
it("Should fail the passerd argument is the hash, not the pre-image", async function () {
325327
await expect(
326328
expect(contract.emitIndexedBytes(str1.bytes))
327329
.to.emit(contract, "WithIndexedBytesArg")
@@ -347,9 +349,7 @@ describe(".to.emit (contract events)", () => {
347349
.withArgs(str1.bytes32)
348350
).to.be.eventually.rejectedWith(
349351
AssertionError,
350-
`expected '${abbrev(
351-
ethers.hexlify(str2.bytes32)
352-
)}' to equal '${abbrev(ethers.hexlify(str1.bytes32))}'`
352+
`Error in "WithBytes32Arg" event: Error in the 1st argument assertion: expected '${str2.abbrev32}' to equal '${str1.abbrev32}'`
353353
);
354354
});
355355
});
@@ -368,17 +368,9 @@ describe(".to.emit (contract events)", () => {
368368
.withArgs(str1.bytes32)
369369
).to.be.eventually.rejectedWith(
370370
AssertionError,
371-
`expected '${abbrev(
372-
ethers.hexlify(str2.bytes32)
373-
)}' to equal '${abbrev(ethers.hexlify(str1.bytes32))}'`
371+
`Error in "WithIndexedBytes32Arg" event: Error in the 1st argument assertion: expected '${str2.abbrev32}' to equal '${str1.abbrev32}'`
374372
);
375373
});
376-
377-
it("Should match the event argument with a hash value", async function () {
378-
await expect(contract.emitIndexedBytes32(str1.bytes32))
379-
.to.emit(contract, "WithIndexedBytes32Arg")
380-
.withArgs(str1.bytes32);
381-
});
382374
});
383375

384376
describe("with a uint array argument", function () {
@@ -395,7 +387,7 @@ describe(".to.emit (contract events)", () => {
395387
.withArgs([3, 4])
396388
).to.be.eventually.rejectedWith(
397389
AssertionError,
398-
"expected 1 to equal 3"
390+
`Error in "WithUintArray" event: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3. The numerical values of the given "bigint" and "number" inputs were compared, and they differed`
399391
);
400392
});
401393

@@ -425,32 +417,27 @@ describe(".to.emit (contract events)", () => {
425417
});
426418

427419
describe("with a bytes32 array argument", function () {
420+
const aa = `0x${"aa".repeat(32)}`;
421+
const bb = `0x${"bb".repeat(32)}`;
422+
const cc = `0x${"cc".repeat(32)}`;
423+
const dd = `0x${"dd".repeat(32)}`;
424+
428425
it("Should succeed when expectations are met", async function () {
429-
await expect(
430-
contract.emitBytes32Array(
431-
`0x${"aa".repeat(32)}`,
432-
`0x${"bb".repeat(32)}`
433-
)
434-
)
426+
await expect(contract.emitBytes32Array(aa, bb))
435427
.to.emit(contract, "WithBytes32Array")
436-
.withArgs([`0x${"aa".repeat(32)}`, `0x${"bb".repeat(32)}`]);
428+
.withArgs([aa, bb]);
437429
});
438430

439431
it("Should fail when expectations are not met", async function () {
440432
await expect(
441-
expect(
442-
contract.emitBytes32Array(
443-
`0x${"aa".repeat(32)}`,
444-
`0x${"bb".repeat(32)}`
445-
)
446-
)
433+
expect(contract.emitBytes32Array(aa, bb))
447434
.to.emit(contract, "WithBytes32Array")
448-
.withArgs([`0x${"cc".repeat(32)}`, `0x${"dd".repeat(32)}`])
435+
.withArgs([cc, dd])
449436
).to.be.eventually.rejectedWith(
450437
AssertionError,
451-
`expected '${abbrev(`0x${"aa".repeat(32)}`)}' to equal '${abbrev(
452-
`0x${"cc".repeat(32)}`
453-
)}'`
438+
`Error in "WithBytes32Array" event: Error in the 1st argument assertion: Error in the 1st argument assertion: expected '${abbrev(
439+
aa
440+
)}' to equal '${abbrev(cc)}'`
454441
);
455442
});
456443
});
@@ -469,7 +456,7 @@ describe(".to.emit (contract events)", () => {
469456
.withArgs([3, 4])
470457
).to.be.eventually.rejectedWith(
471458
AssertionError,
472-
"expected 1 to equal 3"
459+
'Error in "WithStructArg" event: Error in the 1st argument assertion: Error in the 1st argument assertion: expected 1 to equal 3. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
473460
);
474461
});
475462
});
@@ -488,7 +475,7 @@ describe(".to.emit (contract events)", () => {
488475
.withArgs(2, 2)
489476
).to.be.eventually.rejectedWith(
490477
AssertionError,
491-
"expected 1 to equal 2"
478+
'Error in "WithTwoUintArgs" event: Error in the 1st argument assertion: expected 1 to equal 2. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
492479
);
493480
});
494481

@@ -499,7 +486,7 @@ describe(".to.emit (contract events)", () => {
499486
.withArgs(1, 1)
500487
).to.be.eventually.rejectedWith(
501488
AssertionError,
502-
"expected 2 to equal 1"
489+
'Error in "WithTwoUintArgs" event: Error in the 2nd argument assertion: expected 2 to equal 1. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
503490
);
504491
});
505492

@@ -537,7 +524,7 @@ describe(".to.emit (contract events)", () => {
537524
expect(contract.emitTwoUints(1, 2))
538525
.to.emit(contract, "WithTwoUintArgs")
539526
.withArgs(1, () => false)
540-
).to.be.eventually.rejectedWith(
527+
).to.be.rejectedWith(
541528
AssertionError,
542529
'Error in "WithTwoUintArgs" event: Error in the 2nd argument assertion: The predicate did not return true'
543530
);
@@ -550,7 +537,10 @@ describe(".to.emit (contract events)", () => {
550537
.withArgs(() => {
551538
throw new Error("user-defined error");
552539
}, "foo")
553-
).to.be.rejectedWith(Error, "user-defined error");
540+
).to.be.rejectedWith(
541+
Error,
542+
'Error in "WithTwoUintArgs" event: Error in the 1st argument assertion: The predicate threw when called: user-defined error'
543+
);
554544
});
555545

556546
describe("with predicate anyUint", function () {
@@ -655,7 +645,7 @@ describe(".to.emit (contract events)", () => {
655645
.and.to.emit(contract, "WithStringArg")
656646
).to.be.eventually.rejectedWith(
657647
AssertionError,
658-
"expected 1 to equal 2"
648+
'Error in "WithUintArg" event: Error in the 1st argument assertion: expected 1 to equal 2. The numerical values of the given "bigint" and "number" inputs were compared, and they differed'
659649
);
660650
});
661651

@@ -667,7 +657,7 @@ describe(".to.emit (contract events)", () => {
667657
.withArgs("a different string")
668658
).to.be.eventually.rejectedWith(
669659
AssertionError,
670-
"expected 'a string' to equal 'a different string'"
660+
"Error in \"WithStringArg\" event: Error in the 1st argument assertion: expected 'a string' to equal 'a different string'"
671661
);
672662
});
673663

0 commit comments

Comments
 (0)