Skip to content

Commit 2a45e98

Browse files
Add custom error message for better traceability for multiple asserts (#787)
* Add custom error message for better tracebility for multiple asserts Signed-off-by: Maksim Dimitrov <[email protected]> * Fix unrelated readme typo Signed-off-by: Maksim Dimitrov <[email protected]> * Fix typos and test Signed-off-by: Maksim Dimitrov <[email protected]> Signed-off-by: Maksim Dimitrov <[email protected]>
1 parent 725e514 commit 2a45e98

File tree

4 files changed

+96
-96
lines changed

4 files changed

+96
-96
lines changed

packages/server/tests/helpers/assertions.ts

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ export default class Assertions {
3737

3838
static assertId = (id) => {
3939
const [shard, realm, num] = id.split('.');
40-
expect(shard).to.not.be.null;
41-
expect(realm).to.not.be.null;
42-
expect(num).to.not.be.null;
40+
expect(shard, 'Id shard should not be null').to.not.be.null;
41+
expect(realm, 'Id realm should not be null').to.not.be.null;
42+
expect(num, 'Id num should not be null').to.not.be.null;
4343
};
4444

4545
static unsupportedResponse = (resp: any) => {
46-
expect(resp.error.code).to.eq(-32601);
47-
expect(resp.error.message.endsWith('Unsupported JSON-RPC method')).to.be.true;
46+
expect(resp.error.code, 'Unsupported response.error.code should equal -32601').to.eq(-32601);
47+
expect(resp.error.message.endsWith('Unsupported JSON-RPC method'), "Unsupported response.error.code should end with 'Unsupported JSON-RPC method'").to.be.true;
4848
};
4949

5050
static expectedError = () => {
@@ -69,33 +69,33 @@ export default class Assertions {
6969
expect(Number(relayResponse.baseFeePerGas)).to.be.gt(0);
7070
}
7171

72-
expect(relayResponse.difficulty).to.be.equal(ethers.utils.hexValue(0));
73-
expect(relayResponse.extraData).to.be.equal(Assertions.emptyHex);
74-
expect(relayResponse.miner).to.be.equal(ethers.constants.AddressZero);
75-
expect(relayResponse.mixHash).to.be.equal(Assertions.zeroHex32Byte);
76-
expect(relayResponse.nonce).to.be.equal(Assertions.zeroHex8Byte);
77-
expect(relayResponse.receiptsRoot).to.be.equal(Assertions.zeroHex32Byte);
78-
expect(relayResponse.sha3Uncles).to.be.equal(Assertions.emptyArrayHex);
79-
expect(relayResponse.stateRoot).to.be.equal(Assertions.zeroHex32Byte);
80-
expect(relayResponse.totalDifficulty).to.be.equal(ethers.utils.hexValue(0));
81-
expect(relayResponse.uncles).to.be.exist;
82-
expect(relayResponse.uncles.length).to.eq(0);
83-
expect(relayResponse.logsBloom).to.eq(Assertions.emptyBloom);
84-
expect(relayResponse.gasLimit).to.equal(ethers.utils.hexValue(Assertions.maxBlockGasLimit));
72+
expect(relayResponse.difficulty, "Assert block: 'difficulty' should equal zero in hex").to.be.equal(ethers.utils.hexValue(0));
73+
expect(relayResponse.extraData, "Assert block: 'extraDta' should equal empty hex").to.be.equal(Assertions.emptyHex);
74+
expect(relayResponse.miner, "Assert block: 'miner' should equal zero address").to.be.equal(ethers.constants.AddressZero);
75+
expect(relayResponse.mixHash, "Assert block: 'mixHash' should equal zero 32bytes hex").to.be.equal(Assertions.zeroHex32Byte);
76+
expect(relayResponse.nonce, "Assert block: 'nonce' should equal zero 8byte hex").to.be.equal(Assertions.zeroHex8Byte);
77+
expect(relayResponse.receiptsRoot, "Assert block: 'receiptsRoot' should equal zero 32bytes hex").to.be.equal(Assertions.zeroHex32Byte);
78+
expect(relayResponse.sha3Uncles, "Assert block: 'sha3Uncles' should equal empty array hex").to.be.equal(Assertions.emptyArrayHex);
79+
expect(relayResponse.stateRoot, "Assert block: 'stateRoot' should equal zero 32bytes hex").to.be.equal(Assertions.zeroHex32Byte);
80+
expect(relayResponse.totalDifficulty, "Assert block: 'totalDifficulty' should equal zero in hex").to.be.equal(ethers.utils.hexValue(0));
81+
expect(relayResponse.uncles, "Assert block: 'uncles' property exists").to.be.exist;
82+
expect(relayResponse.uncles.length, "Assert block: 'uncles' length should equal 0").to.eq(0);
83+
expect(relayResponse.logsBloom, "Assert block: 'logsBloom' should equal emptyBloom").to.eq(Assertions.emptyBloom);
84+
expect(relayResponse.gasLimit, "Assert block: 'gasLimit' should equal 'maxBlockGasLimit'").to.equal(ethers.utils.hexValue(Assertions.maxBlockGasLimit));
8585

8686
// Assert dynamic values
87-
expect(relayResponse.hash).to.be.equal(mirrorNodeResponse.hash.slice(0, 66));
88-
expect(relayResponse.number).to.be.equal(ethers.utils.hexValue(mirrorNodeResponse.number));
89-
expect(relayResponse.transactions.length).to.equal(mirrorTransactions.length);
90-
expect(relayResponse.parentHash).to.equal(mirrorNodeResponse.previous_hash.slice(0, 66));
91-
expect(relayResponse.size).to.equal(ethers.utils.hexValue(mirrorNodeResponse.size | 0));
92-
expect(relayResponse.gasUsed).to.equal(ethers.utils.hexValue(mirrorNodeResponse.gas_used));
93-
expect(relayResponse.timestamp).to.equal(ethers.utils.hexValue(Number(mirrorNodeResponse.timestamp.from.split('.')[0])));
87+
expect(relayResponse.hash, "Assert block: 'hash' should equal mirrorNode response").to.be.equal(mirrorNodeResponse.hash.slice(0, 66));
88+
expect(relayResponse.number, "Assert block: 'hash' should equal mirrorNode response").to.be.equal(ethers.utils.hexValue(mirrorNodeResponse.number));
89+
expect(relayResponse.transactions.length, "Assert block: 'transactions' count should equal mirrorNode response").to.equal(mirrorTransactions.length);
90+
expect(relayResponse.parentHash, "Assert block: 'parentHash' should equal mirrorNode response").to.equal(mirrorNodeResponse.previous_hash.slice(0, 66));
91+
expect(relayResponse.size, "Assert block: 'size' should equal mirrorNode response").to.equal(ethers.utils.hexValue(mirrorNodeResponse.size | 0));
92+
expect(relayResponse.gasUsed, "Assert block: 'gasUsed' should equal mirrorNode response").to.equal(ethers.utils.hexValue(mirrorNodeResponse.gas_used));
93+
expect(relayResponse.timestamp, "Assert block: 'timestamp' should equal mirrorNode response").to.equal(ethers.utils.hexValue(Number(mirrorNodeResponse.timestamp.from.split('.')[0])));
9494
if (relayResponse.transactions.length) {
95-
expect(relayResponse.transactionsRoot).to.equal(mirrorNodeResponse.hash.slice(0, 66));
95+
expect(relayResponse.transactionsRoot, "Assert block: 'transactionsRoot' should equal mirrorNode response").to.equal(mirrorNodeResponse.hash.slice(0, 66));
9696
}
9797
else {
98-
expect(relayResponse.transactionsRoot).to.equal(Assertions.ethEmptyTrie);
98+
expect(relayResponse.transactionsRoot, "Assert block: 'transactionsRoot' should equal 'ethEmptyTrie'").to.equal(Assertions.ethEmptyTrie);
9999
}
100100

101101
// Assert transactions
@@ -112,87 +112,87 @@ export default class Assertions {
112112
}
113113

114114
public static transaction(relayResponse, mirrorNodeResponse) {
115-
expect(relayResponse.blockHash).to.eq(mirrorNodeResponse.block_hash.slice(0, 66));
116-
expect(relayResponse.blockNumber).to.eq(ethers.utils.hexValue(mirrorNodeResponse.block_number));
115+
expect(relayResponse.blockHash, "Assert transaction: 'blockHash' should equal mirrorNode response").to.eq(mirrorNodeResponse.block_hash.slice(0, 66));
116+
expect(relayResponse.blockNumber, "Assert transaction: 'blockNumber' should equal mirrorNode response").to.eq(ethers.utils.hexValue(mirrorNodeResponse.block_number));
117117
// expect(relayResponse.chainId).to.eq(mirrorNodeResponse.chain_id); // FIXME must not be null!
118-
expect(relayResponse.from).to.eq(mirrorNodeResponse.from);
119-
expect(relayResponse.gas).to.eq(ethers.utils.hexValue(mirrorNodeResponse.gas_used));
118+
expect(relayResponse.from, "Assert transaction: 'from' should equal mirrorNode response").to.eq(mirrorNodeResponse.from);
119+
expect(relayResponse.gas, "Assert transaction: 'gas' should equal mirrorNode response").to.eq(ethers.utils.hexValue(mirrorNodeResponse.gas_used));
120120
// expect(relayResponse.gasPrice).to.eq(mirrorNodeResponse.gas_price); // FIXME must not be null!
121-
expect(relayResponse.hash).to.eq(mirrorNodeResponse.hash.slice(0, 66));
122-
expect(relayResponse.input).to.eq(mirrorNodeResponse.function_parameters);
121+
expect(relayResponse.hash, "Assert transaction: 'hash' should equal mirrorNode response").to.eq(mirrorNodeResponse.hash.slice(0, 66));
122+
expect(relayResponse.input, "Assert transaction: 'input' should equal mirrorNode response").to.eq(mirrorNodeResponse.function_parameters);
123123
if (relayResponse.to || mirrorNodeResponse.to) {
124-
expect(relayResponse.to).to.eq(mirrorNodeResponse.to);
124+
expect(relayResponse.to, "Assert transaction: 'to' should equal mirrorNode response").to.eq(mirrorNodeResponse.to);
125125
}
126-
expect(relayResponse.transactionIndex).to.eq(ethers.utils.hexValue(mirrorNodeResponse.transaction_index));
127-
expect(relayResponse.value).to.eq(ethers.utils.hexValue(mirrorNodeResponse.amount));
126+
expect(relayResponse.transactionIndex, "Assert transaction: 'transactionIndex' should equal mirrorNode response").to.eq(ethers.utils.hexValue(mirrorNodeResponse.transaction_index));
127+
expect(relayResponse.value, "Assert transaction: 'value' should equal mirrorNode response").to.eq(ethers.utils.hexValue(mirrorNodeResponse.amount));
128128
}
129129

130130
static transactionReceipt = (transactionReceipt, mirrorResult) => {
131-
expect(transactionReceipt.blockHash).to.exist;
132-
expect(transactionReceipt.blockHash).to.not.eq('0x0');
133-
expect(transactionReceipt.blockHash).to.eq(mirrorResult.block_hash.slice(0, 66));
131+
expect(transactionReceipt.blockHash, "Assert transactionReceipt: 'blockHash' should exists").to.exist;
132+
expect(transactionReceipt.blockHash, "Assert transactionReceipt: 'blockHash' should not be 0x0").to.not.eq('0x0');
133+
expect(transactionReceipt.blockHash, "Assert transactionReceipt: 'vablockHashlue' should equal mirrorNode response").to.eq(mirrorResult.block_hash.slice(0, 66));
134134

135-
expect(transactionReceipt.blockNumber).to.exist;
136-
expect(Number(transactionReceipt.blockNumber)).to.gt(0);
137-
expect(transactionReceipt.blockNumber).to.eq(ethers.utils.hexValue(mirrorResult.block_number));
135+
expect(transactionReceipt.blockNumber, "Assert transactionReceipt: 'blockNumber' should exist").to.exist;
136+
expect(Number(transactionReceipt.blockNumber), "Assert transactionReceipt: 'blockNumber' should be > 0").to.gt(0);
137+
expect(transactionReceipt.blockNumber, "Assert transactionReceipt: 'blockNumber' should equal mirrorNode response").to.eq(ethers.utils.hexValue(mirrorResult.block_number));
138138

139-
expect(transactionReceipt.cumulativeGasUsed).to.exist;
140-
expect(Number(transactionReceipt.cumulativeGasUsed)).to.gt(0);
141-
expect(Number(transactionReceipt.cumulativeGasUsed)).to.eq(mirrorResult.block_gas_used);
139+
expect(transactionReceipt.cumulativeGasUsed, "Assert transactionReceipt: 'cumulativeGasUsed' should exist").to.exist;
140+
expect(Number(transactionReceipt.cumulativeGasUsed), "Assert transactionReceipt: 'cumulativeGasUsed' should be > 0").to.gt(0);
141+
expect(Number(transactionReceipt.cumulativeGasUsed), "Assert transactionReceipt: 'cumulativeGasUsed' should equal mirrorNode response").to.eq(mirrorResult.block_gas_used);
142142

143-
expect(transactionReceipt.gasUsed).to.exist;
144-
expect(Number(transactionReceipt.gasUsed)).to.gt(0);
145-
expect(Number(transactionReceipt.gasUsed)).to.eq(mirrorResult.gas_used);
143+
expect(transactionReceipt.gasUsed, "Assert transactionReceipt: 'gasUsed' should exist").to.exist;
144+
expect(Number(transactionReceipt.gasUsed), "Assert transactionReceipt: 'gasUsed' should be > 0").to.gt(0);
145+
expect(Number(transactionReceipt.gasUsed), "Assert transactionReceipt: 'gasUsed' should equal mirrorNode response").to.eq(mirrorResult.gas_used);
146146

147-
expect(transactionReceipt.logsBloom).to.exist;
148-
expect(transactionReceipt.logsBloom).to.not.eq('0x0');
149-
expect(transactionReceipt.logsBloom).to.eq(mirrorResult.bloom);
147+
expect(transactionReceipt.logsBloom, "Assert transactionReceipt: 'logsBloom' should exist").to.exist;
148+
expect(transactionReceipt.logsBloom, "Assert transactionReceipt: 'logsBloom' should not be 0x0").to.not.eq('0x0');
149+
expect(transactionReceipt.logsBloom, "Assert transactionReceipt: 'logsBloom' should equal mirrorNode response").to.eq(mirrorResult.bloom);
150150

151-
expect(transactionReceipt.transactionHash).to.exist;
152-
expect(transactionReceipt.transactionHash).to.not.eq('0x0');
153-
expect(transactionReceipt.transactionHash).to.eq(mirrorResult.hash);
151+
expect(transactionReceipt.transactionHash, "Assert transactionReceipt: 'transactionHash' should exist").to.exist;
152+
expect(transactionReceipt.transactionHash, "Assert transactionReceipt: 'transactionHash' should equal mirrorNode response").to.not.eq('0x0');
153+
expect(transactionReceipt.transactionHash, "Assert transactionReceipt: 'transactionHash' should equal mirrorNode response").to.eq(mirrorResult.hash);
154154

155-
expect(transactionReceipt.transactionIndex).to.exist;
156-
expect(Number(transactionReceipt.transactionIndex)).to.eq(mirrorResult.transaction_index);
155+
expect(transactionReceipt.transactionIndex, "Assert transactionReceipt: 'transactionIndex' should exist").to.exist;
156+
expect(Number(transactionReceipt.transactionIndex), "Assert transactionReceipt: 'transactionIndex' should equal mirrorNode response").to.eq(mirrorResult.transaction_index);
157157

158-
expect(transactionReceipt.effectiveGasPrice).to.exist;
159-
expect(Number(transactionReceipt.effectiveGasPrice)).to.gt(0);
158+
expect(transactionReceipt.effectiveGasPrice, "Assert transactionReceipt: 'effectiveGasPrice' should exist").to.exist;
159+
expect(Number(transactionReceipt.effectiveGasPrice), "Assert transactionReceipt: 'effectiveGasPrice' should be > 0").to.gt(0);
160160
const effectiveGas = mirrorResult.max_fee_per_gas === undefined || mirrorResult.max_fee_per_gas == '0x'
161161
? mirrorResult.gas_price
162162
: mirrorResult.max_fee_per_gas;
163163
const mirrorEffectiveGasPrice = Utils.tinyBarsToWeibars(effectiveGas);
164-
expect(BigNumber.from(transactionReceipt.effectiveGasPrice).toString()).to.eq(mirrorEffectiveGasPrice.toString());
164+
expect(BigNumber.from(transactionReceipt.effectiveGasPrice).toString(), "Assert transactionReceipt: 'effectiveGasPrice' should equal mirrorNode response").to.eq(mirrorEffectiveGasPrice.toString());
165165

166-
expect(transactionReceipt.status).to.exist;
167-
expect(transactionReceipt.status).to.eq(mirrorResult.status);
166+
expect(transactionReceipt.status, "Assert transactionReceipt: 'status' should exist").to.exist;
167+
expect(transactionReceipt.status, "Assert transactionReceipt: 'status' should equal mirrorNode response").to.eq(mirrorResult.status);
168168

169-
expect(transactionReceipt.logs).to.exist;
170-
expect(transactionReceipt.logs.length).to.eq(mirrorResult.logs.length);
171-
expect(transactionReceipt.logs).to.deep.eq(mirrorResult.logs);
169+
expect(transactionReceipt.logs, "Assert transactionReceipt: 'logs' should exist").to.exist;
170+
expect(transactionReceipt.logs.length, "Assert transactionReceipt: 'logs' count should equal to mirrorNode response").to.eq(mirrorResult.logs.length);
171+
expect(transactionReceipt.logs, "Assert transactionReceipt: 'logs' should equal mirrorNode response").to.deep.eq(mirrorResult.logs);
172172

173-
expect(transactionReceipt.from).to.eq(mirrorResult.from);
173+
expect(transactionReceipt.from, "Assert transactionReceipt: 'from' should equal mirrorNode response").to.eq(mirrorResult.from);
174174

175-
expect(transactionReceipt.to).to.eq(mirrorResult.to);
175+
expect(transactionReceipt.to, "Assert transactionReceipt: 'to' should equal mirrorNode response").to.eq(mirrorResult.to);
176176
};
177177

178178
public static feeHistory(res: any, expected: any) {
179-
expect(res.baseFeePerGas).to.exist.to.be.an('Array');
180-
expect(res.gasUsedRatio).to.exist.to.be.an('Array');
181-
expect(res.oldestBlock).to.exist;
182-
expect(res.baseFeePerGas.length).to.equal(expected.resultCount + 1);
183-
expect(res.gasUsedRatio.length).to.equal(expected.resultCount);
179+
expect(res.baseFeePerGas, "Assert feeHistory: 'baseFeePerGas' should exist and be an Array").to.exist.to.be.an('Array');
180+
expect(res.gasUsedRatio, "Assert feeHistory: 'gasUsedRatio' should exist and be an Array").to.exist.to.be.an('Array');
181+
expect(res.oldestBlock, "Assert feeHistory: 'oldestBlock' should exist").to.exist;
182+
expect(res.baseFeePerGas.length, "Assert feeHistory: 'baseFeePerGas' length should equal passed expected value").to.equal(expected.resultCount + 1);
183+
expect(res.gasUsedRatio.length, "Assert feeHistory: 'gasUsedRatio' length should equal passed expected value").to.equal(expected.resultCount);
184184

185-
expect(res.oldestBlock).to.equal(expected.oldestBlock);
185+
expect(res.oldestBlock, "Assert feeHistory: 'oldestBlock' should equal passed expected value").to.equal(expected.oldestBlock);
186186

187-
res.gasUsedRatio.map((gasRatio: string) => expect(gasRatio).to.equal(`0x${Assertions.defaultGasUsed.toString(16)}`))
187+
res.gasUsedRatio.map((gasRatio: string) => expect(gasRatio, "Assert feeHistory: 'gasRatio' should equal 'defaultGasUsed'").to.equal(`0x${Assertions.defaultGasUsed.toString(16)}`));
188188

189189
if (expected.checkReward) {
190-
expect(res.reward).to.exist.to.be.an('Array');
191-
expect(res.reward.length).to.equal(expected.resultCount);
190+
expect(res.reward, "Assert feeHistory: 'reward' should exist and be an Array").to.exist.to.be.an('Array');
191+
expect(res.reward.length, "Assert feeHistory: 'reward' length should equal passed expected value").to.equal(expected.resultCount);
192192
}
193193
}
194194

195-
static unknownResponse(err) {
195+
static unknownResponse(err: any) {
196196
Assertions.jsonRpcError(err, predefined.INTERNAL_ERROR());
197197
}
198198

packages/server/tests/helpers/nodeCheck.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const net = require("net");
77
const isLocalNode = !supportedEnvs.includes(network.toLowerCase());
88
if (isLocalNode) {
99
let nodeStarted = false;
10-
let retries = 10;
10+
const retries = 10;
1111
while (!nodeStarted && retries >= 0) {
1212
net
1313
.createConnection('5600', '127.0.0.1')

0 commit comments

Comments
 (0)