Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add .on('error') test for reverted contract methods #6194

Merged
merged 6 commits into from
Jun 21, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ describe('contract errors', () => {
});
});

it('should catch Unauthorized error PromiEvent.on("error")', async () => {
const expectedThrownError = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice if we have this const expectedThrownError = {... above and we use it inside the 2 related test cases which related to the Unauthorized() error.

name: 'ContractExecutionError',
code: ERR_CONTRACT_EXECUTION_REVERTED,
receipt: undefined,
innerError: {
code: 3,
data: '0x82b42900',
errorName: 'Unauthorized',
errorSignature: 'Unauthorized()',
},
};

await expect(
deployedContract.methods
.unauthorize()
.send(sendOptions)
.on('error', error => expect(error).toMatchObject(expectedThrownError)),
).rejects.toMatchObject(expectedThrownError);
});

it('Error with parameter', async () => {
let error: ContractExecutionError | undefined;
try {
Expand All @@ -101,5 +122,27 @@ describe('contract errors', () => {
},
});
});

it('should catch error with parameter using PromiEvent.on("error")', async () => {
const expectedThrownError = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice if we have this const expectedThrownError = {... above and we use it inside the 2 related test cases which related to the CustomError error.

name: 'ContractExecutionError',
code: ERR_CONTRACT_EXECUTION_REVERTED,
receipt: undefined,
innerError: {
code: 3,
data: '0x8d6ea8be0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001b7265766572746564207573696e6720637573746f6d204572726f720000000000',
errorName: 'CustomError',
errorSignature: 'CustomError(string)',
errorArgs: { '0': 'reverted using custom Error' },
},
};

await expect(
deployedContract.methods
.badRequire()
.send(sendOptions)
.on('error', error => expect(error).toMatchObject(expectedThrownError)),
).rejects.toMatchObject(expectedThrownError);
});
});
});