Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/rakannimer/create-function-mock
- Owner: rakannimer
- Created: 2018-06-24T11:02:34.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-06-24T11:27:11.000Z (over 6 years ago)
- Last Synced: 2024-12-16T22:13:55.318Z (2 months ago)
- Language: JavaScript
- Homepage: https://codesandbox.io/s/github/rakannimer/create-function-mock
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.js
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
};