Skip to content

Commit 0603eb9

Browse files
committed
fix revertedWithCustomError, round 2
1 parent b9563a5 commit 0603eb9

File tree

2 files changed

+67
-43
lines changed

2 files changed

+67
-43
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type EthersT from "ethers";
22
import ordinal from "ordinal";
3-
import { AssertionError } from "chai";
43
import { AssertWithSsfi, Ssfi } from "../utils";
54
import { PREVIOUS_MATCHER_NAME } from "./constants";
65
import {
@@ -62,7 +61,6 @@ export function assertArgsArraysEqual(
6261
ssfi
6362
);
6463
} catch (err: any) {
65-
// throw new AssertionError("Fix this");
6664
err.message = `Error in ${tag}: ${err.message}`;
6765
throw err;
6866
}

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

+67-41
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ import { anyUint, anyValue } from "../../src/withArgs";
1616
import { MatchersContract } from "../contracts";
1717

1818
describe("INTEGRATION: Reverted with custom error", function () {
19-
describe("with the in-process hardhat network", function () {
19+
describe.skip("with the in-process hardhat network", function () {
2020
useEnvironment("hardhat-project");
2121

2222
runTests();
2323
});
2424

25-
describe.only("connected to a hardhat node", function () {
25+
describe("connected to a hardhat node", function () {
2626
useEnvironmentWithNode("hardhat-project");
2727

2828
runTests();
@@ -223,54 +223,80 @@ describe("INTEGRATION: Reverted with custom error", function () {
223223
});
224224

225225
describe("with args", function () {
226-
it("should work with one argument", async function () {
227-
await expect(matchers.revertWithCustomErrorWithUint(1))
228-
.to.be.revertedWithCustomError(matchers, "CustomErrorWithUint")
229-
.withArgs(1);
230-
231-
await expect(
232-
expect(matchers.revertWithCustomErrorWithUint(1))
226+
describe("one argument", async function () {
227+
it("Should match correct argument", async function () {
228+
await expect(matchers.revertWithCustomErrorWithUint(1))
233229
.to.be.revertedWithCustomError(matchers, "CustomErrorWithUint")
234-
.withArgs(2)
235-
).to.be.rejectedWith(AssertionError, "expected 1 to equal 2");
230+
.withArgs(1);
231+
});
232+
233+
it("Should fail if wrong argument", async function () {
234+
await expect(
235+
expect(matchers.revertWithCustomErrorWithUint(1))
236+
.to.be.revertedWithCustomError(matchers, "CustomErrorWithUint")
237+
.withArgs(2)
238+
).to.be.rejectedWith(
239+
AssertionError,
240+
'Error in "CustomErrorWithUint" custom error: 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'
241+
);
242+
});
236243
});
237244

238-
it("should work with two arguments", async function () {
239-
await expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
240-
.to.be.revertedWithCustomError(
241-
matchers,
242-
"CustomErrorWithUintAndString"
245+
describe("two arguments", function () {
246+
it("Should match correct values", async function () {
247+
await expect(
248+
matchers.revertWithCustomErrorWithUintAndString(1, "foo")
243249
)
244-
.withArgs(1, "foo");
245-
246-
await expect(
247-
expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
248250
.to.be.revertedWithCustomError(
249251
matchers,
250252
"CustomErrorWithUintAndString"
251253
)
252-
.withArgs(2, "foo")
253-
).to.be.rejectedWith(AssertionError, "expected 1 to equal 2");
254+
.withArgs(1, "foo");
255+
});
254256

255-
await expect(
256-
expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
257-
.to.be.revertedWithCustomError(
258-
matchers,
259-
"CustomErrorWithUintAndString"
260-
)
261-
.withArgs(1, "bar")
262-
).to.be.rejectedWith(AssertionError, "expected 'foo' to equal 'bar'");
257+
it("Should fail if uint is wrong", async function () {
258+
await expect(
259+
expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
260+
.to.be.revertedWithCustomError(
261+
matchers,
262+
"CustomErrorWithUintAndString"
263+
)
264+
.withArgs(2, "foo")
265+
).to.be.rejectedWith(
266+
AssertionError,
267+
'Error in "CustomErrorWithUintAndString" custom error: 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'
268+
);
269+
});
263270

264-
await expect(
265-
expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
266-
.to.be.revertedWithCustomError(
267-
matchers,
268-
"CustomErrorWithUintAndString"
269-
)
270-
.withArgs(() => {
271-
throw new Error("user-defined error");
272-
}, "foo")
273-
).to.be.rejectedWith(Error, "user-defined error");
271+
it("Should fail if string is wrong", async function () {
272+
await expect(
273+
expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
274+
.to.be.revertedWithCustomError(
275+
matchers,
276+
"CustomErrorWithUintAndString"
277+
)
278+
.withArgs(1, "bar")
279+
).to.be.rejectedWith(
280+
AssertionError,
281+
"Error in \"CustomErrorWithUintAndString\" custom error: Error in the 2nd argument assertion: expected 'foo' to equal 'bar'"
282+
);
283+
});
284+
285+
it("Should if first prefdicate fails", async function () {
286+
await expect(
287+
expect(matchers.revertWithCustomErrorWithUintAndString(1, "foo"))
288+
.to.be.revertedWithCustomError(
289+
matchers,
290+
"CustomErrorWithUintAndString"
291+
)
292+
.withArgs(() => {
293+
throw new Error("user-defined error");
294+
}, "foo")
295+
).to.be.rejectedWith(
296+
Error,
297+
'Error in "CustomErrorWithUintAndString" custom error: Error in the 1st argument assertion: The predicate threw when called: user-defined error'
298+
);
299+
});
274300
});
275301

276302
describe("different number of arguments", function () {
@@ -317,7 +343,7 @@ describe("INTEGRATION: Reverted with custom error", function () {
317343
.withArgs([3, 2])
318344
).to.be.rejectedWith(
319345
AssertionError,
320-
'expected 1 to equal 3. The numerical values of the given "bigint" and "number" inputs were compared, and they differed.'
346+
'Error in "CustomErrorWithPair" custom error: 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'
321347
);
322348
});
323349
});

0 commit comments

Comments
 (0)