fake-function
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

Fake function

Npm package version Small size

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

Readme

Keywords

Package Sidebar

Install

npm i fake-function

Weekly Downloads

5

Version

1.0.9

License

MIT

Unpacked Size

7.23 kB

Total Files

10

Last publish

Collaborators

  • neki-development