Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rakannimer/create-function-mock

Created with CodeSandbox
https://github.com/rakannimer/create-function-mock

Last synced: 12 days ago
JSON representation

Created with CodeSandbox

Awesome Lists containing this project

README

        

/**
Usage
const asyncFunctionToMock = () => {
return new Promise(resolve => {
setTimeout(resolve, 500);
}).then(() => "resolved");
};
const functionToMock = () => {
console.log('I will not be called')
};
const mockedSyncFunction = createFunctionMock(functionToMock);
const mockedAsyncFunction = createFunctionMock(asyncFunctionToMock);

mockedFunction();
console.log(mockedFunction.mock);
*/

const createFunctionMock = (mockImplementation = () => {}) => {
const mock = {
calls: []
};
const mockedFunction = (...params) => {
const mockReturn = mockImplementation(...params);
mock.calls.push({ params });
return mockReturn;
};
mockedFunction.mock = mock;
mockedFunction.getCalls = () => mock.calls;
return mockedFunction;
};

module.exports = {
createFunctionMock
};