Skip to content

Commit bf50263

Browse files
committed
Accept more kind of values in revertedWithPanic
1 parent 7773251 commit bf50263

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

packages/hardhat-chai-matchers/src/reverted/revertedWithPanic.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1+
import { BigNumber } from "ethers";
2+
import { normalizeToBigInt } from "hardhat/common";
13
import { panicErrorCodeToReason } from "./panic";
24
import { decodeReturnData, getReturnDataFromError } from "./utils";
35

46
export function supportRevertedWithPanic(Assertion: Chai.AssertionStatic) {
57
Assertion.addMethod(
68
"revertedWithPanic",
7-
function (this: any, expectedCode: unknown) {
9+
function (this: any, expectedCodeArg: any) {
810
const ethers = require("ethers");
911

10-
// validate expected code
11-
if (
12-
expectedCode !== undefined &&
13-
typeof expectedCode !== "number" &&
14-
!ethers.BigNumber.isBigNumber(expectedCode)
15-
) {
12+
let expectedCode: BigNumber | undefined;
13+
try {
14+
if (expectedCodeArg !== undefined) {
15+
const normalizedCode = normalizeToBigInt(expectedCodeArg);
16+
expectedCode = BigNumber.from(normalizedCode);
17+
}
18+
} catch {
1619
throw new TypeError(
17-
"Expected a number or BigNumber as the expected panic code"
20+
`Expected a number-like value as the expected panic code, but got '${expectedCodeArg}'`
1821
);
1922
}
2023

packages/hardhat-chai-matchers/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ declare namespace Chai {
88
reverted: AsyncAssertion;
99
revertedWith(reason: string): AsyncAssertion;
1010
revertedWithoutReasonString(): AsyncAssertion;
11-
revertedWithPanic(code?: number): AsyncAssertion;
11+
revertedWithPanic(code?: any): AsyncAssertion;
1212
revertedWithCustomError(
1313
contract: { interface: any },
1414
customErrorName: string

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

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AssertionError, expect } from "chai";
2+
import { BigNumber } from "ethers";
23
import { ProviderError } from "hardhat/internal/core/providers/errors";
34

45
import "../../src";
@@ -239,6 +240,42 @@ describe("INTEGRATION: Reverted with panic", function () {
239240
});
240241
});
241242

243+
describe("accepted panic code values", function () {
244+
it("number", async function () {
245+
await runSuccessfulAsserts({
246+
matchers,
247+
method: "succeeds",
248+
successfulAssert: (x) => expect(x).not.to.be.revertedWithPanic(1),
249+
});
250+
});
251+
252+
it("bigint", async function () {
253+
await runSuccessfulAsserts({
254+
matchers,
255+
method: "succeeds",
256+
successfulAssert: (x) =>
257+
expect(x).not.to.be.revertedWithPanic(BigInt(1)),
258+
});
259+
});
260+
261+
it("string", async function () {
262+
await runSuccessfulAsserts({
263+
matchers,
264+
method: "succeeds",
265+
successfulAssert: (x) => expect(x).not.to.be.revertedWithPanic("1"),
266+
});
267+
});
268+
269+
it("ethers's BigNumber", async function () {
270+
await runSuccessfulAsserts({
271+
matchers,
272+
method: "succeeds",
273+
successfulAssert: (x) =>
274+
expect(x).not.to.be.revertedWithPanic(BigNumber.from(1)),
275+
});
276+
});
277+
});
278+
242279
describe("invalid values", function () {
243280
it("non-errors as subject", async function () {
244281
await expect(
@@ -249,12 +286,9 @@ describe("INTEGRATION: Reverted with panic", function () {
249286
it("non-number as expectation", async function () {
250287
const { hash } = await mineSuccessfulTransaction(this.hre);
251288

252-
expect(() =>
253-
// @ts-expect-error
254-
expect(hash).to.be.revertedWithPanic("10")
255-
).to.throw(
289+
expect(() => expect(hash).to.be.revertedWithPanic("invalid")).to.throw(
256290
TypeError,
257-
"Expected a number or BigNumber as the expected panic code"
291+
"Expected a number-like value as the expected panic code, but got 'invalid'"
258292
);
259293
});
260294

0 commit comments

Comments
 (0)