npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

function-mock

v2.0.2

Published

Create a mock function which will return values based on arguments it's received

Downloads

2

Readme

function-mock

Intro

The intention of this module is to simplify mock functions in unit tests.

Sometimes it's not required or even detrimental to use tools, which allow calls counting, arguments recording etc. If you don't need all that functionality you may use an approach similar to this one: just define a behavior for a function, when it's called with particular arguments.

const functionMock = require('function-mock');
const fm = functionMock()
  .with('foo').returns('1')
  .with('bar').returns('2')
  .withOther().throws('Some error message');
//returns '1'
fm('foo');
//returns '2'
fm('bar');
//throws
fm('bar', true);

Documentation v2.0.*

The imported module is a function, which when called returns a mock function. On itself it's not much useful.

const functionMock = require('function-mock');
const mocked = functionMock();

//returns undefined
mocked();

mocked function's methods

This function has several methods:

  • .with(arg1?, arg2? …) Accepts arguments which later will be matched against arguments provided to the mocked(arg1?, arg2? …) calls.

  • .withNew(arg1?, arg2? …) The same as the .with() method, except that arguments will be matched only if the mocked function is called with the new operator: new mocked(arg1?, arg2 …)

  • .withOther() Used to provide a return or throw value for ordinary calls (without the new operator) where the arguments don't match any of the arguments previously provided with the .with() or .withNew() calls the mocked(arg1?, arg2? …) calls.

    const functionMock = require('function-mock');
    let mocked = functionMock()
      .with(1).returns(2)
      .withOther().returns(0);
    //returns 0
    mocked('not mathing value');
    //returns 0
    mocked();
    
    mocked = functionMock()
      .with(1).returns(2)
      .withOther().throws('Some error message');
    //throws 0
    mocked();
    //etc.

    This method is supposed to be one of the last ones in the call chain. After it's called only the .withOtherNew() method will be available on the value returned from the .returns() or .throws() call.

      const mocked = functionMock()
        .with(1).returns(2)
        .withOther().returns(0);
        // this will throw, since no .with or .withNew methods available at
        //this point
        .with()
        .returns(1);
  • .withOtherNew() The same as the .withOther() method, except that will return/throw a value/error with a message provided to the subsequent .returns() or .throws() call only if the mock function has been called with the new operator.

  • .returns(value) This method is intended to be called after a .with() or similar method call: .with(…).returns(…). Makes the mocked function return the provided value if the arguments to its call match, (if called after .withNew() then the value must be an object (not a primitive)):

    const functionMock = require('function-mock');
    const mocked = functionMock().with(1).returns(2);
    //returns 2
    mocked(1);
    
    const notPrimitive = [];
    mocked.withNew(1).returns(notPrimitive);
    //returns notPrimitive
    new mocked(1);
  • .throws(message) analogous to .returns() but throws new Error(message).

Call behavior with unmatched arguments

  • without the new operator, when the .withOther() method hasn't been called:

    the mock function returns undefined if it's called with arguments which haven't matched arguments from any previous .with() call:

    const mocked = functionMock()
      .with(1).returns(2)
      .with(1, 2).returns(3);
    
    //returns undefined
    mocked(2, 1);
  • without the new operator, when the .withOther() method has been called:

    the mock function returns a value provided to a subsequent .returns() call, or throws an error with a message, provided to a subsequent .throws() call

    const mocked = functionMock()
      .with(1).returns(2)
      .with(1, 2).returns(3)
      .withOther().returns('foo');
    
    //returns 'foo'
    mocked(2, 1);
  • with the new operator, when the .withOtherNew() method hasn't been called:

    the mock function returns undefined if it's called with arguments which haven't matched arguments from any previous .withNew() call:

    const retValueOne = [];
    const retValueTwo = {};
    const mocked = functionMock()
      .withNew(1).returns(retValueOne)
      .withNew(1, 2).returns(retValueTwo);
    
    //returns {} (an instance of mocked)
    new mocked(2, 1);
  • with the new operator, when the .withOtherNew() method has been called:

    the mock function returns/throws a value/error with a message provided to the subsequent .returns() / .throws() call:

    const retValueOne = [];
    const retValueTwo = {};
    const retValueDefault = {};
    const mocked = functionMock()
      .withNew(1).returns(retValueOne)
      .withNew(1, 2).returns(retValueTwo)
      .withOtherNew().returns(retValueDefault);
    
    //returns retValueDefault
    new mocked(2, 1);

Arguments match rules

For all methods (.with() & .withNew()) the arguments matching rules are the same (the only difference is that if a mocked function was called with or without the new operator) its arguments won't ever match arguments provided to the .withNew() / .with() methods respectively.

For instance:

const mocked = functionMock().with(1).returns(2);
//doesn't match, doesn't return 2
new mocked(1);

mocked.withNew(2).returns({});
//doesn't match, doesn't return {}
mocked(2);
  • the argument order matters:

    const fm = functionMock().with(1, 2).returns(3);
    //doesn't match, doesn't return 3
    fm(2, 1);
  • the amount of arguments matters:

    const fm = functionMock().with(1).returns(3);
    //doesn't match, doesn't return 3
    fm(1, 2);
  • primitive values are compared strictly ===

  • reference values are compared deeply by their leaf nodes:

    const fm = functionMock().with([{foo: 'foo'}], 'bar').returns('match');
    //matches, returns 'match'
    fm([{foo: 'foo',}], 'bar');
  • prototypes are counted, for instance:

    class A {
      constructor() {}
    }
    
    //new A() will virtually produce a {} (an empty object w/o any own
    //properties) but it won't match a litteral {}
    const fm = functionMock().with(new A()).returns('match');
    //doesn't match
    fm({});
    //matches, returns 'match'
    fm(new A());

LICENSE

MIT