Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit a368780

Browse files
authored
Merge branch '4.x' into ok/6500-Errors-not-caught-in-4.x
2 parents 4f5bd10 + 7461c8e commit a368780

File tree

8 files changed

+146
-12
lines changed

8 files changed

+146
-12
lines changed

codecov.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ignore:
2+
- "./packages/web3-core/src/formatters.ts" # ignore deprecated formatters from coverage report
13
coverage:
24
status:
35
project:
@@ -75,4 +77,4 @@ comment:
7577
behavior: 'default'
7678
require_changes: false
7779
require_base: no
78-
require_head: no
80+
require_head: no

packages/web3-core/src/web3_config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export abstract class Web3Config
104104
// TODO: Improve and add key check
105105
const keys = Object.keys(options) as (keyof Web3ConfigOptions)[];
106106
for (const key of keys) {
107-
this._triggerConfigChange(key, options[key])
107+
this._triggerConfigChange(key, options[key]);
108108
}
109109
Object.assign(this.config, options);
110110
}

packages/web3-core/src/web3_request_manager.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ export class Web3RequestManager<
187187
'Provider not available. Use `.setProvider` or `.provider=` to initialize the provider.',
188188
);
189189
}
190-
190+
191191
const payload = jsonRpc.isBatchRequest(request)
192-
? jsonRpc.toBatchPayload(request)
192+
? jsonRpc.toBatchPayload(request)
193193
: jsonRpc.toPayload(request);
194-
194+
195195
if (isWeb3Provider(provider)) {
196196
let response;
197197

@@ -292,7 +292,7 @@ export class Web3RequestManager<
292292

293293
if (isNullish(response)) {
294294
throw new ResponseError(
295-
'' as never,
295+
{} as never,
296296
'Got a "nullish" response from provider.',
297297
);
298298
}

packages/web3-core/test/unit/formatters.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
inputBlockNumberFormatter,
2424
inputDefaultBlockNumberFormatter,
2525
inputPostFormatter,
26+
inputTopicFormatter,
2627
outputBigIntegerFormatter,
2728
outputBlockFormatter,
2829
outputLogFormatter,
@@ -82,6 +83,17 @@ describe('formatters', () => {
8283
});
8384
});
8485

86+
describe('inputTopicFormatter', () => {
87+
it('check params', () => {
88+
expect(inputTopicFormatter('0x09d7bD9E185fbC2d265D8DBe81e5e888E391688b')).toBe(
89+
'0x09d7bD9E185fbC2d265D8DBe81e5e888E391688b',
90+
);
91+
// @ts-expect-error invalid param
92+
// eslint-disable-next-line no-null/no-null
93+
expect(inputTopicFormatter(null)).toBeNull();
94+
});
95+
});
96+
8597
describe('outputBigIntegerFormatter', () => {
8698
it('should convert input to number', () => {
8799
const result = outputBigIntegerFormatter(BigInt(12));
@@ -320,6 +332,12 @@ describe('formatters', () => {
320332
describe('outputTransactionReceiptFormatter', () => {
321333
const validReceipt = { cumulativeGasUsed: '0x1234', gasUsed: '0x4567' };
322334

335+
it('should be FormatterError', () => {
336+
// @ts-expect-error invalid param
337+
expect(() => outputTransactionReceiptFormatter(1)).toThrow(
338+
`Received receipt is invalid: 1`,
339+
);
340+
});
323341
it('should convert "blockNumber" from hex to number', () => {
324342
const result = outputTransactionReceiptFormatter({
325343
...validReceipt,
@@ -381,6 +399,11 @@ describe('formatters', () => {
381399
expect(result.logs).toEqual(['outputLogFormatterResult', 'outputLogFormatterResult']);
382400
});
383401

402+
it('when log doesn`t have id', () => {
403+
const res = formatters.outputLogFormatter({ blockHash: '0x1', logIndex: '0x1' });
404+
expect(res.id).toBeUndefined();
405+
});
406+
384407
it('should convert "contractAddress" to checksum address', () => {
385408
const result = outputTransactionReceiptFormatter({
386409
...validReceipt,

packages/web3-core/test/unit/web3_batch_request.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ describe('Web3BatchRequest', () => {
3939
});
4040

4141
describe('constructor', () => {
42-
it('should create batch request object with empty list of requests', () => {
42+
it('should create batch request object with empty list of requests', async () => {
4343
expect(batchRequest).toBeInstanceOf(Web3BatchRequest);
4444
expect(batchRequest.requests).toEqual([]);
45+
expect(batchRequest.requests).toHaveLength(0);
46+
expect(await batchRequest.execute()).toEqual([]);
4547
});
4648
});
4749

packages/web3-core/test/unit/web3_config.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ describe('Web3Config', () => {
105105
},
106106
);
107107

108+
it('set default chain error', () => {
109+
const obj = new MyConfigObject();
110+
111+
obj.setConfig({
112+
// @ts-expect-error incorrect object
113+
defaultCommon: {
114+
baseChain: 'mainnet',
115+
},
116+
});
117+
expect(() => {
118+
obj.defaultChain = 'test';
119+
}).toThrow(
120+
'Web3Config chain doesnt match in defaultHardfork mainnet and common.hardfork test',
121+
);
122+
});
123+
108124
it('Updating transactionPollingInterval should update transactionReceiptPollingInterval and transactionConfirmationPollingInterval', () => {
109125
const obj = new MyConfigObject();
110126
const configChange = jest.fn();

packages/web3-core/test/unit/web3_promi_event.test.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe('Web3PromiEvent', () => {
2424
});
2525

2626
await expect(p).resolves.toBe('Resolved Value');
27+
expect(() => p.removeAllListeners()).not.toThrow();
2728
});
2829

2930
it('should initialize and reject promise', async () => {
@@ -69,12 +70,16 @@ describe('Web3PromiEvent', () => {
6970
};
7071

7172
const p = func();
72-
73-
// eslint-disable-next-line no-void
74-
void p.on('data', data => {
73+
const eventFunc = (data: string) => {
7574
expect(data).toBe('emitted data');
7675
done(undefined);
77-
});
76+
expect(() => p.off('data', eventFunc)).not.toThrow();
77+
};
78+
// eslint-disable-next-line no-void
79+
void p.on('data', eventFunc);
80+
expect(p.listenerCount('data')).toBe(1);
81+
expect(p.listeners('data')).toHaveLength(1);
82+
expect(p.eventNames()).toEqual(['data']);
7883
});
7984
});
8085

@@ -122,4 +127,27 @@ describe('Web3PromiEvent', () => {
122127

123128
expect(p.getMaxListeners()).toBe(3);
124129
});
130+
131+
it('finally', async () => {
132+
const p = new Web3PromiEvent(resolve => {
133+
return resolve('reason');
134+
});
135+
136+
const f = jest.fn();
137+
p.finally(f);
138+
await p;
139+
expect(f).toHaveBeenCalled();
140+
});
141+
it('catch', async () => {
142+
const f = jest.fn();
143+
const p = new Web3PromiEvent((_, reject) => {
144+
return reject(new Error('reason'));
145+
});
146+
147+
p.catch(f);
148+
149+
await expect(p).rejects.toThrow('reason');
150+
151+
expect(f).toHaveBeenCalledWith(new Error('reason'));
152+
});
125153
});

packages/web3-core/test/unit/web3_request_manager.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,31 @@ describe('Web3RequestManager', () => {
6666
expect(manager).toBeInstanceOf(Web3RequestManager);
6767
});
6868
});
69+
describe('isMetaMaskProvider', () => {
70+
it('check params', () => {
71+
const request = {
72+
constructor: {
73+
name: 'AsyncFunction',
74+
},
75+
};
6976

77+
expect(
78+
utils.isMetaMaskProvider({
79+
// @ts-expect-error incorrect param
80+
request,
81+
isMetaMask: true,
82+
}),
83+
).toBe(true);
84+
});
85+
});
86+
describe('isSupportSubscriptions', () => {
87+
it('check params', () => {
88+
// @ts-expect-error incorrect param
89+
expect(utils.isSupportSubscriptions({ supportsSubscriptions: () => true })).toBe(true);
90+
// @ts-expect-error incorrect param
91+
expect(utils.isSupportSubscriptions({})).toBe(false);
92+
});
93+
});
7094
describe('providers', () => {
7195
it('should return providers on instance', () => {
7296
const manager = new Web3RequestManager();
@@ -97,6 +121,11 @@ describe('Web3RequestManager', () => {
97121
});
98122
});
99123

124+
it('should unset provider', () => {
125+
const manager = new Web3RequestManager();
126+
manager.setProvider(undefined);
127+
expect(manager.provider).toBeUndefined();
128+
});
100129
it('should detect and set http provider', () => {
101130
const providerString = 'http://mydomain.com';
102131

@@ -235,10 +264,44 @@ describe('Web3RequestManager', () => {
235264

236265
it('should throw error if no provider is set', async () => {
237266
const manager = new Web3RequestManager();
238-
239267
await expect(manager.send(request)).rejects.toThrow('Provider not available');
240268
});
241269

270+
it('promise of legacy provider should be resolved', async () => {
271+
const manager = new Web3RequestManager(undefined, undefined);
272+
const pr = new Promise(resolve => {
273+
resolve('test');
274+
});
275+
const myProvider = {
276+
request: jest.fn().mockImplementation(async () => pr),
277+
} as any;
278+
manager.setProvider(myProvider);
279+
await manager.send(request);
280+
expect(myProvider.request).toHaveBeenCalledTimes(1);
281+
expect(await pr).toBe('test');
282+
});
283+
it('Got a "nullish" response from provider', async () => {
284+
const manager = new Web3RequestManager(undefined, undefined);
285+
const myProvider = {
286+
send: jest.fn().mockImplementation((_, cb: (error?: any, data?: any) => void) => {
287+
cb(undefined, undefined);
288+
}),
289+
} as any;
290+
manager.setProvider(myProvider);
291+
292+
await expect(async () => manager.send(request)).rejects.toThrow(
293+
'Got a "nullish" response from provider',
294+
);
295+
});
296+
it('Provider does not have a request or send method to use', async () => {
297+
const manager = new Web3RequestManager(undefined, undefined);
298+
const myProvider = {} as any;
299+
manager.setProvider(myProvider);
300+
301+
await expect(async () => manager.send(request)).rejects.toThrow(
302+
'Provider does not have a request or send method to use.',
303+
);
304+
});
242305
describe('test rpc errors', () => {
243306
it('should pass request to provider and reject with a generic rpc error when rpc call specification flag is undefined', async () => {
244307
const parseErrorResponse = {

0 commit comments

Comments
 (0)