fake-function
v1.0.9
Published
Fake function wrapper for tests
Downloads
10
Maintainers
Readme
Fake function
Fake function wrapper for tests
.
Install
npm i fake-function --save-dev
Usage
/**
* Get wrapped function by original function
*/
fake(
// Original function
sourceFunction: Function,
// Declarations for fake results
declarations: {
// Function arguments for match
args: any[],
// Function return value
return: any,
}[]
): Function
/**
* Get original function by wrapped function
*/
unfake(
// Wrapped function
fakedFunction: Function,
): Function
Example
const { fake, unfake } = require('fake-function');
function someFunction(a, b) {
return a + b;
}
someFunction = fake(someFunction, [
{args: [10, 20], return: 1},
{args: [10, 50], return: 2},
{args: [20, 50], return: 3},
]);
someFunction(10, 20); // return 2
someFunction(10, 50); // return 2
someFunction = unfake(someFunction);
someFunction(10, 20); // return 30