Improve customError testing (#4376)

This commit is contained in:
Hadrien Croubois
2023-06-20 18:34:10 +02:00
committed by GitHub
parent b66c77a1fc
commit dac2457a80

View File

@ -2,19 +2,19 @@ const { expect } = require('chai');
/** Revert handler that supports custom errors. */ /** Revert handler that supports custom errors. */
async function expectRevertCustomError(promise, expectedErrorName, args) { async function expectRevertCustomError(promise, expectedErrorName, args) {
try {
await promise;
expect.fail("Expected promise to throw but it didn't");
} catch (revert) {
if (!Array.isArray(args)) { if (!Array.isArray(args)) {
expect.fail('Expected 3rd array parameter for error arguments'); expect.fail('Expected 3rd array parameter for error arguments');
} }
await promise.then(
() => expect.fail("Expected promise to throw but it didn't"),
({ message }) => {
// The revert message for custom errors looks like: // The revert message for custom errors looks like:
// VM Exception while processing transaction: // VM Exception while processing transaction:
// reverted with custom error 'InvalidAccountNonce("0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 0)' // reverted with custom error 'InvalidAccountNonce("0x70997970C51812dc3A010C7d01b50e0d17dc79C8", 0)'
// We trim out anything inside the single quotes as comma-separated values // We trim out anything inside the single quotes as comma-separated values
const [, error] = revert.message.match(/'(.*)'/); const [, error] = message.match(/'(.*)'/);
// Attempt to parse as an error // Attempt to parse as an error
const match = error.match(/(?<name>\w+)\((?<args>.*)\)/); const match = error.match(/(?<name>\w+)\((?<args>.*)\)/);
@ -37,7 +37,8 @@ async function expectRevertCustomError(promise, expectedErrorName, args) {
// Assert argument equality // Assert argument equality
expect(sanitizedActual).to.have.members(sanitizedExpected, `Unexpected ${errorName} arguments`); expect(sanitizedActual).to.have.members(sanitizedExpected, `Unexpected ${errorName} arguments`);
} },
);
} }
module.exports = { module.exports = {