Skip to content

Commit 5a85926

Browse files
committed
Move input validation into it's own fn
1 parent ce274dc commit 5a85926

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

packages/hardhat-chai-matchers/src/internal/changeEtherBalances.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,7 @@ export function supportChangeEtherBalances(
3737
chaiUtils
3838
);
3939

40-
if (
41-
Array.isArray(balanceChanges) &&
42-
accounts.length !== balanceChanges.length
43-
) {
44-
throw new Error(
45-
`The number of accounts (${accounts.length}) is different than the number of expected balance changes (${balanceChanges.length})`
46-
);
47-
}
40+
validateInput(this._obj, accounts, balanceChanges);
4841

4942
const checkBalanceChanges = ([actualChanges, accountAddresses]: [
5043
bigint[],
@@ -113,6 +106,28 @@ export function supportChangeEtherBalances(
113106
);
114107
}
115108

109+
function validateInput(
110+
obj: any,
111+
accounts: Array<Addressable | string>,
112+
balanceChanges: EthersT.BigNumberish[] | ((changes: bigint[]) => boolean)
113+
) {
114+
try {
115+
if (
116+
Array.isArray(balanceChanges) &&
117+
accounts.length !== balanceChanges.length
118+
) {
119+
throw new Error(
120+
`The number of accounts (${accounts.length}) is different than the number of expected balance changes (${balanceChanges.length})`
121+
);
122+
}
123+
} catch (e) {
124+
// if the input validation fails, we discard the subject since it could
125+
// potentially be a rejected promise
126+
Promise.resolve(obj).catch(() => {});
127+
throw e;
128+
}
129+
}
130+
116131
export async function getBalanceChanges(
117132
transaction: TransactionResponse | Promise<TransactionResponse>,
118133
accounts: Array<Addressable | string>,

0 commit comments

Comments
 (0)