Skip to content

Commit dac2457

Browse files
authored
Improve customError testing (#4376)
1 parent b66c77a commit dac2457

File tree

1 file changed

+36
-35
lines changed

1 file changed

+36
-35
lines changed

test/helpers/customError.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,43 @@ const { expect } = require('chai');
22

33
/** Revert handler that supports custom errors. */
44
async function expectRevertCustomError(promise, expectedErrorName, args) {
5-
try {
6-
await promise;
7-
expect.fail("Expected promise to throw but it didn't");
8-
} catch (revert) {
9-
if (!Array.isArray(args)) {
10-
expect.fail('Expected 3rd array parameter for error arguments');
11-
}
12-
// The revert message for custom errors looks like:
13-
// VM Exception while processing transaction:
14-
// reverted with custom error 'InvalidAccountNonce("0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 0)'
15-
16-
// We trim out anything inside the single quotes as comma-separated values
17-
const [, error] = revert.message.match(/'(.*)'/);
18-
19-
// Attempt to parse as an error
20-
const match = error.match(/(?<name>\w+)\((?<args>.*)\)/);
21-
if (!match) {
22-
expect.fail(`Couldn't parse "${error}" as a custom error`);
23-
}
24-
// Extract the error name and parameters
25-
const errorName = match.groups.name;
26-
const argMatches = [...match.groups.args.matchAll(/-?\w+/g)];
27-
28-
// Assert error name
29-
expect(errorName).to.be.equal(
30-
expectedErrorName,
31-
`Unexpected custom error name (with found args: [${argMatches.map(([a]) => a)}])`,
32-
);
33-
34-
// Coerce to string for comparison since `arg` can be either a number or hex.
35-
const sanitizedExpected = args.map(arg => arg.toString().toLowerCase());
36-
const sanitizedActual = argMatches.map(([arg]) => arg.toString().toLowerCase());
37-
38-
// Assert argument equality
39-
expect(sanitizedActual).to.have.members(sanitizedExpected, `Unexpected ${errorName} arguments`);
5+
if (!Array.isArray(args)) {
6+
expect.fail('Expected 3rd array parameter for error arguments');
407
}
8+
9+
await promise.then(
10+
() => expect.fail("Expected promise to throw but it didn't"),
11+
({ message }) => {
12+
// The revert message for custom errors looks like:
13+
// VM Exception while processing transaction:
14+
// reverted with custom error 'InvalidAccountNonce("0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 0)'
15+
16+
// We trim out anything inside the single quotes as comma-separated values
17+
const [, error] = message.match(/'(.*)'/);
18+
19+
// Attempt to parse as an error
20+
const match = error.match(/(?<name>\w+)\((?<args>.*)\)/);
21+
if (!match) {
22+
expect.fail(`Couldn't parse "${error}" as a custom error`);
23+
}
24+
// Extract the error name and parameters
25+
const errorName = match.groups.name;
26+
const argMatches = [...match.groups.args.matchAll(/-?\w+/g)];
27+
28+
// Assert error name
29+
expect(errorName).to.be.equal(
30+
expectedErrorName,
31+
`Unexpected custom error name (with found args: [${argMatches.map(([a]) => a)}])`,
32+
);
33+
34+
// Coerce to string for comparison since `arg` can be either a number or hex.
35+
const sanitizedExpected = args.map(arg => arg.toString().toLowerCase());
36+
const sanitizedActual = argMatches.map(([arg]) => arg.toString().toLowerCase());
37+
38+
// Assert argument equality
39+
expect(sanitizedActual).to.have.members(sanitizedExpected, `Unexpected ${errorName} arguments`);
40+
},
41+
);
4142
}
4243

4344
module.exports = {

0 commit comments

Comments
 (0)