From 9bc3133b840881fc400cb4b2b43dd11e908a47a4 Mon Sep 17 00:00:00 2001 From: Luc Grandin <104393342+GrandinLuc@users.noreply.github.com> Date: Wed, 26 Feb 2025 15:02:33 +0100 Subject: [PATCH 1/3] feat: added generic mock HTTP client for unit tests --- .../thor/blocks/RetrieveRawBlock.unit.test.ts | 23 +++++------------ .../blocks/RetrieveRegularBlock.unit.test.ts | 21 ++++------------ .../QuerySmartContractEvents.unit.test.ts | 18 +++---------- .../logs/QueryVETTransferEvents.unit.test.ts | 19 +++----------- .../node/RetrieveConnectedPeers.unit.test.ts | 25 ++++--------------- .../thorest/tests/utils/MockUnitTestClient.ts | 18 +++++++++++++ 6 files changed, 40 insertions(+), 84 deletions(-) create mode 100644 packages/thorest/tests/utils/MockUnitTestClient.ts diff --git a/packages/thorest/tests/thor/blocks/RetrieveRawBlock.unit.test.ts b/packages/thorest/tests/thor/blocks/RetrieveRawBlock.unit.test.ts index aed60d5b9..ad8965015 100644 --- a/packages/thorest/tests/thor/blocks/RetrieveRawBlock.unit.test.ts +++ b/packages/thorest/tests/thor/blocks/RetrieveRawBlock.unit.test.ts @@ -1,23 +1,11 @@ import { describe, test, expect } from '@jest/globals'; -import { type FetchHttpClient } from '../../../src/http'; import { type RawBlockResponseJSON, RawBlockResponse, RetrieveRawBlock } from '../../../src/thor/blocks'; import { Revision } from '@vechain/sdk-core'; - -const mockHttpClient = (response: T): FetchHttpClient => { - return { - get: jest.fn().mockImplementation(() => { - return { - json: jest.fn().mockImplementation(() => { - return response; - }) - }; - }) - } as unknown as FetchHttpClient; -}; +import { mockHttpClient } from '../../utils/MockUnitTestClient'; /** * VeChain raw block - unit @@ -25,14 +13,14 @@ const mockHttpClient = (response: T): FetchHttpClient => { * @group unit/block */ describe('RetrieveBlock unit tests', () => { - test('should obtain raw block successfully', async () => { + test('should obtain raw block successfully 2', async () => { const mockRawBlock: RawBlockResponseJSON = { raw: '0x123' } satisfies RawBlockResponseJSON; const mockRawBlockResponse = await RetrieveRawBlock.of( Revision.BEST - ).askTo(mockHttpClient(mockRawBlock)); + ).askTo(mockHttpClient(mockRawBlock, 'get')); expect(mockRawBlockResponse.response.toJSON()).toEqual( new RawBlockResponse(mockRawBlock).toJSON() ); @@ -43,8 +31,9 @@ describe('RetrieveBlock unit tests', () => { await expect( RetrieveRawBlock.of(Revision.BEST).askTo( - mockHttpClient( - mockIncompleteRawBlock as RawBlockResponseJSON + mockHttpClient( + mockIncompleteRawBlock as RawBlockResponseJSON, + 'get' ) ) ).rejects.toThrowError( diff --git a/packages/thorest/tests/thor/blocks/RetrieveRegularBlock.unit.test.ts b/packages/thorest/tests/thor/blocks/RetrieveRegularBlock.unit.test.ts index e05956b40..cc765c1ce 100644 --- a/packages/thorest/tests/thor/blocks/RetrieveRegularBlock.unit.test.ts +++ b/packages/thorest/tests/thor/blocks/RetrieveRegularBlock.unit.test.ts @@ -1,23 +1,11 @@ import { describe, test, expect } from '@jest/globals'; -import { type FetchHttpClient } from '../../../src/http'; import { RetrieveRegularBlock, type RegularBlockResponseJSON, RegularBlockResponse } from '../../../src/thor/blocks'; import { Revision } from '@vechain/sdk-core'; - -const mockHttpClient = (response: T): FetchHttpClient => { - return { - get: jest.fn().mockImplementation(() => { - return { - json: jest.fn().mockImplementation(() => { - return response; - }) - }; - }) - } as unknown as FetchHttpClient; -}; +import { mockHttpClient } from '../../utils/MockUnitTestClient'; /** * VeChain regular block - unit @@ -49,7 +37,7 @@ describe('RetrieveBlock unit tests', () => { const mockRegularBlockResponse = await RetrieveRegularBlock.of( Revision.BEST - ).askTo(mockHttpClient(mockRegularBlock)); + ).askTo(mockHttpClient(mockRegularBlock, 'get')); expect(mockRegularBlockResponse.response.toJSON()).toEqual( new RegularBlockResponse(mockRegularBlock).toJSON() ); @@ -63,8 +51,9 @@ describe('RetrieveBlock unit tests', () => { await expect( RetrieveRegularBlock.of(Revision.BEST).askTo( - mockHttpClient( - mockIncompleteRegularBlock as RegularBlockResponseJSON + mockHttpClient( + mockIncompleteRegularBlock as RegularBlockResponseJSON, + 'get' ) ) ).rejects.toThrowError( diff --git a/packages/thorest/tests/thor/logs/QuerySmartContractEvents.unit.test.ts b/packages/thorest/tests/thor/logs/QuerySmartContractEvents.unit.test.ts index d36214aa9..b7015ac1e 100644 --- a/packages/thorest/tests/thor/logs/QuerySmartContractEvents.unit.test.ts +++ b/packages/thorest/tests/thor/logs/QuerySmartContractEvents.unit.test.ts @@ -1,23 +1,11 @@ import { describe, test } from '@jest/globals'; import { QuerySmartContractEvents } from '../../../src/thor/logs/QuerySmartContractEvents'; import { type EventLogFilterRequestJSON } from '../../../src/thor/logs/EventLogFilterRequest'; -import { type FetchHttpClient } from '../../../src'; import { EventLogsResponse, type EventLogsResponseJSON } from '../../../src/thor/logs'; - -const mockHttpClient = (response: T): FetchHttpClient => { - return { - post: jest.fn().mockImplementation(() => { - return { - json: jest.fn().mockImplementation(() => { - return response; - }) - }; - }) - } as unknown as FetchHttpClient; -}; +import { mockHttpClient } from '../../utils/MockUnitTestClient'; /** *VeChain node - unit @@ -66,7 +54,7 @@ describe('QuerySmartContractEvents unit tests', () => { } ] satisfies EventLogsResponseJSON; - const mockClient = mockHttpClient(mockResponse); + const mockClient = mockHttpClient(mockResponse, 'post'); const response = await QuerySmartContractEvents.of(request).askTo(mockClient); @@ -95,7 +83,7 @@ describe('QuerySmartContractEvents unit tests', () => { order: 'asc' }; - const mockClient = mockHttpClient([]); + const mockClient = mockHttpClient([], 'post'); const response = await QuerySmartContractEvents.of(request).askTo(mockClient); diff --git a/packages/thorest/tests/thor/logs/QueryVETTransferEvents.unit.test.ts b/packages/thorest/tests/thor/logs/QueryVETTransferEvents.unit.test.ts index d7c61e9e1..c92fa0e92 100644 --- a/packages/thorest/tests/thor/logs/QueryVETTransferEvents.unit.test.ts +++ b/packages/thorest/tests/thor/logs/QueryVETTransferEvents.unit.test.ts @@ -1,23 +1,11 @@ import { describe, test } from '@jest/globals'; import { QueryVETTransferEvents } from '../../../src/thor/logs/QueryVETTransferEvents'; import { type TransferLogFilterRequestJSON } from '../../../src/thor/logs/TransferLogFilterRequest'; -import { type FetchHttpClient } from '../../../src'; import { TransferLogsResponse, type TransferLogsResponseJSON } from '../../../src/thor/logs'; - -const mockHttpClient = (response: T): FetchHttpClient => { - return { - post: jest.fn().mockImplementation(() => { - return { - json: jest.fn().mockImplementation(() => { - return response; - }) - }; - }) - } as unknown as FetchHttpClient; -}; +import { mockHttpClient } from '../../utils/MockUnitTestClient'; /** *VeChain node - unit @@ -66,8 +54,7 @@ describe('QueryVETTransferEvents unit tests', () => { } ] satisfies TransferLogsResponseJSON; - const mockClient = - mockHttpClient(mockResponse); + const mockClient = mockHttpClient(mockResponse, 'post'); const response = await QueryVETTransferEvents.of(request).askTo(mockClient); @@ -97,7 +84,7 @@ describe('QueryVETTransferEvents unit tests', () => { order: 'asc' }; - const mockClient = mockHttpClient([]); + const mockClient = mockHttpClient([], 'post'); const response = await QueryVETTransferEvents.of(request).askTo(mockClient); diff --git a/packages/thorest/tests/thor/node/RetrieveConnectedPeers.unit.test.ts b/packages/thorest/tests/thor/node/RetrieveConnectedPeers.unit.test.ts index e260cc387..554eddf2c 100644 --- a/packages/thorest/tests/thor/node/RetrieveConnectedPeers.unit.test.ts +++ b/packages/thorest/tests/thor/node/RetrieveConnectedPeers.unit.test.ts @@ -1,21 +1,6 @@ import { describe, test } from '@jest/globals'; -import { - type FetchHttpClient, - type PeerStatJSON, - RetrieveConnectedPeers -} from '../../../src'; - -const mockHttpClient = (response: T): FetchHttpClient => { - return { - get: jest.fn().mockImplementation(() => { - return { - json: jest.fn().mockImplementation(() => { - return response; - }) - }; - }) - } as unknown as FetchHttpClient; -}; +import { type PeerStatJSON, RetrieveConnectedPeers } from '../../../src'; +import { mockHttpClient } from '../../utils/MockUnitTestClient'; /** *VeChain node - unit @@ -45,15 +30,15 @@ describe('RetrieveConnectedPeers unit tests', () => { inbound: true, duration: 37 } - ]; + ] satisfies PeerStatJSON[]; const mockPeersResponse = await new RetrieveConnectedPeers().askTo( - mockHttpClient(mockPeers) + mockHttpClient(mockPeers, 'get') ); expect(mockPeersResponse.response.toJSON()).toEqual(mockPeers); const emptyPeersResponse = await new RetrieveConnectedPeers().askTo( - mockHttpClient([]) + mockHttpClient([], 'get') ); expect(emptyPeersResponse.response.toJSON()).toEqual([]); }); diff --git a/packages/thorest/tests/utils/MockUnitTestClient.ts b/packages/thorest/tests/utils/MockUnitTestClient.ts new file mode 100644 index 000000000..f991c12da --- /dev/null +++ b/packages/thorest/tests/utils/MockUnitTestClient.ts @@ -0,0 +1,18 @@ +import { type FetchHttpClient } from '../../src'; + +const mockHttpClient = ( + response: T, + httpMethod: 'get' | 'post' +): FetchHttpClient => { + return { + [httpMethod]: jest.fn().mockImplementation(() => { + return { + json: jest.fn().mockImplementation(() => { + return response satisfies T; + }) + }; + }) + } as unknown as FetchHttpClient; +}; + +export { mockHttpClient }; From 0840ee9d0028ba7e769083f79dd836a74689977a Mon Sep 17 00:00:00 2001 From: Luc Grandin <104393342+GrandinLuc@users.noreply.github.com> Date: Thu, 27 Feb 2025 10:27:43 +0100 Subject: [PATCH 2/3] fix: changed from FetchHttpClient to HttpClient for the mockHttpClient --- packages/thorest/tests/utils/MockUnitTestClient.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/thorest/tests/utils/MockUnitTestClient.ts b/packages/thorest/tests/utils/MockUnitTestClient.ts index f991c12da..c6fb5917b 100644 --- a/packages/thorest/tests/utils/MockUnitTestClient.ts +++ b/packages/thorest/tests/utils/MockUnitTestClient.ts @@ -1,9 +1,9 @@ -import { type FetchHttpClient } from '../../src'; +import { type HttpClient } from '../../src'; const mockHttpClient = ( response: T, httpMethod: 'get' | 'post' -): FetchHttpClient => { +): HttpClient => { return { [httpMethod]: jest.fn().mockImplementation(() => { return { @@ -12,7 +12,7 @@ const mockHttpClient = ( }) }; }) - } as unknown as FetchHttpClient; + } as unknown as HttpClient; }; export { mockHttpClient }; From 550ad8b57b139d682ff6848c1fc1c81f301ca715 Mon Sep 17 00:00:00 2001 From: Luc Grandin <104393342+GrandinLuc@users.noreply.github.com> Date: Thu, 27 Feb 2025 16:12:16 +0100 Subject: [PATCH 3/3] fix: removed unnecessary jest mock implementation function --- packages/thorest/tests/utils/MockUnitTestClient.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/thorest/tests/utils/MockUnitTestClient.ts b/packages/thorest/tests/utils/MockUnitTestClient.ts index c6fb5917b..fa984ac87 100644 --- a/packages/thorest/tests/utils/MockUnitTestClient.ts +++ b/packages/thorest/tests/utils/MockUnitTestClient.ts @@ -5,13 +5,13 @@ const mockHttpClient = ( httpMethod: 'get' | 'post' ): HttpClient => { return { - [httpMethod]: jest.fn().mockImplementation(() => { + [httpMethod]: () => { return { - json: jest.fn().mockImplementation(() => { + json: () => { return response satisfies T; - }) + } }; - }) + } } as unknown as HttpClient; };