Skip to content

Commit

Permalink
302 unit tests retry execute function (#305)
Browse files Browse the repository at this point in the history
* 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
windushka and julienbrs authored Jun 26, 2023
1 parent dda39d6 commit aed8d8f
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions test/utils/retryExecuteFunction.spec.ts
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, {});
});
});

0 comments on commit aed8d8f

Please sign in to comment.