-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
302 unit tests retry execute function (#305)
* added unit tests for retryExecuteFunction.ts * adding console error mock * add missing args parameter tests * fix lint --------- Co-authored-by: julienbrs <[email protected]>
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { JSON_RPC_REQUEST_METHOD } from '../../src/interfaces/JsonRpcMethods'; | ||
import { trySafeExecute } from '../../src/utils/retryExecuteFunction'; | ||
import { wait } from '../../src/utils/time'; | ||
|
||
jest.mock('../../src/utils/time'); | ||
|
||
describe('trySafeExecute function', () => { | ||
beforeAll(() => { | ||
const consoleSpy = jest.spyOn(console, 'error'); | ||
consoleSpy.mockImplementation(() => null); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.spyOn(global, 'setTimeout'); | ||
(wait as jest.Mock).mockImplementation((delay: number) => { | ||
return new Promise((resolve) => setTimeout(resolve, delay)); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should execute a function successfully on the first attempt', async () => { | ||
const mockFunc = jest.fn().mockResolvedValue('success'); | ||
const result = await trySafeExecute(mockFunc, [ | ||
JSON_RPC_REQUEST_METHOD.GET_STATUS, | ||
{}, | ||
]); | ||
expect(result).toEqual('success'); | ||
expect(mockFunc).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should retry the function upon failure and succeed', async () => { | ||
const mockFunc = jest | ||
.fn() | ||
.mockRejectedValueOnce(new Error('failed')) | ||
.mockResolvedValue('success'); | ||
const result = await trySafeExecute(mockFunc, [ | ||
JSON_RPC_REQUEST_METHOD.GET_STATUS, | ||
{}, | ||
]); | ||
expect(result).toEqual('success'); | ||
expect(mockFunc).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('should retry the function the correct number of times and then throw an error', async () => { | ||
const mockFunc = jest.fn().mockRejectedValue(new Error('failed')); | ||
await expect( | ||
trySafeExecute(mockFunc, [JSON_RPC_REQUEST_METHOD.GET_STATUS, {}], 3), | ||
).rejects.toThrow('failed'); | ||
expect(mockFunc).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should throw an error when no function is provided', async () => { | ||
await expect( | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
trySafeExecute(null as any, [JSON_RPC_REQUEST_METHOD.GET_STATUS, {}]), | ||
).rejects.toThrow(`Function execution init conditions are erroneous: null`); | ||
}); | ||
|
||
it('should handle missing args parameter', async () => { | ||
const mockFunc = jest.fn().mockResolvedValue('success'); | ||
const result = await trySafeExecute(mockFunc); | ||
expect(result).toEqual('success'); | ||
expect(mockFunc).toHaveBeenCalledWith(null, {}); | ||
}); | ||
}); |