Skip to content

Commit

Permalink
Add .on('error') test for reverted contract methods (#6194)
Browse files Browse the repository at this point in the history
* Add a test for .on('error') for contract method error

* Add a test for .on('error') for contract method error

* Update packages/web3-eth-contract/test/integration/contract_methods_errors.test.ts

Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>

* Update packages/web3-eth-contract/test/integration/contract_methods_errors.test.ts

Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>

* Rename error codes

---------

Co-authored-by: Junaid <86780488+jdevcs@users.noreply.github.com>
Co-authored-by: Muhammad Altabba <24407834+Muhammad-Altabba@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 21, 2023
1 parent 023f02d commit a2a232f
Showing 1 changed file with 43 additions and 0 deletions.
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 = {
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 = {
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);
});
});
});

0 comments on commit a2a232f

Please sign in to comment.