Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] isFunction function for async functions #1857

Merged
merged 6 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ export function typeOf(object: any): string {
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isFunction(fn: any): fn is Function {
return typeOf(fn) === 'function';
export function isFunction(fn: any): fn is (...args: any[]) => any {
return typeOf(fn) === 'function' || typeOf(fn) === 'asyncfunction';
}

/**
Expand Down
63 changes: 63 additions & 0 deletions test/cypress/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { isFunction } from '../../../src/components/utils';

function syncFunction(): void {}

async function asyncFunction(): Promise<void> {}

const syncArrowFunction = (): void => {};

const asyncArrowFunction = async (): Promise<void> => {};

describe('isFunction function', () => {
it('should recognise sync functions', () => {
/**
* Act
*/
const commonFunctionResult = isFunction(syncFunction);
const arrowFunctionResult = isFunction(syncArrowFunction);

/**
* Assert
*/
expect(commonFunctionResult).to.eq(true);
expect(arrowFunctionResult).to.eq(true);
});

it('should recognise async functions', () => {
/**
* Act
*/
const commonFunctionResult = isFunction(asyncFunction);
const arrowFunctionResult = isFunction(asyncArrowFunction);

/**
* Assert
*/
expect(commonFunctionResult).to.eq(true);
expect(arrowFunctionResult).to.eq(true);
});

it('should return false if it isn\'t a function', () => {
/**
* Arrange
*/
const obj = {};
const num = 123;
const str = '123';

/**
* Act
*/
const objResult = isFunction(obj);
const numResult = isFunction(num);
const strResult = isFunction(str);

/**
* Assert
*/
expect(objResult).to.eq(false);
expect(numResult).to.eq(false);
expect(strResult).to.eq(false);
});
});