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

mockuire

v2.1.0

Published

require a module with mocked dependencies

Downloads

38

Readme

This is not a mocking library. This module allows you to require a module and pass mocks for its dependencies.

Dependencies that are not passed will be solved normally.

This module uses vm.runInNewContext and is heavily inspired by this answer in stackoverflow answer.

Install

npm install mockuire

Usage

Given a file like this one foo.js:

var path = require("path");

module.exports = function(a, b){
    return path.join(a, b, "burbujas");
};

then we can test as follows:

  var mockuire = require("mockuire")(module);

  exports.test = function (test) {
    //this is the "mock" of the module path 
    var mockedPath = { 
      join: function() { 
          return Array.prototype.slice.call(arguments, 0).join("!"); 
        } 
      };

    //now I "mockuire" the module under test with the mocked path
    var foo = mockuire("./fixture/foo", { path: mockedPath });

    //let's see if it works:
    result = foo( "a", "b" );

    test.equal( result, "a!b!burbujas" );
  }

You have to pass the module in order to fallback to the module require when needed.

Private members

Two new methods will be added to the instance returned by mockuire.

Given a file like private.js:

var count = 1;
var data = {
  foo: { x: 10 },
  someFn: function() { return false; }

};

function ping() {
  return 'pong';
}

module.exports.inc = function() {
  return ++count;
};

module.exports.testPing = function() {
  return ping();
}

module.exports.cloneFoo = function() {
  return Object.assign({}, data.foo);
};

module.exports.invoke = function() {
  return data.someFn();
};

method: _private_get(name)

It allows you to get the value of a private variable:

  • name (Array|string): The path of the property to get.
it ('should be able to get value of a private evariable', function() {
  var mockuire = require("mockuire")(module);
  var private = mockuire("./fixture/private");

  assert.equal(private._private_get('count'), 1);
  assert.equal(private._private_get(['data', 'foo', 'x']), 10);
});

method: _private_set(name, value)

It allows you to set the value of a private variable:

  • name (Array|string): The path of the property to get.
it ('should be able to get value of a private evariable', function() {
  var mockuire = require("mockuire")(module);
  var private = mockuire("./fixture/private");

  private._private_set('count', 10);
  assert.equal(private.inc(), 11);

  private._private_set(['data', 'foo', 'x'], 20);
  assert.equal(private.cloneFoo().x, 20);
});

You can also set private members in the same moment you pass mocks for its dependencies

it('should be able to set value of a private evariable', function() {
  var mockuire = require("../lib/index")(module);
  var mocks = {};
  var props = {
    count: 100
  };
  var private = mockuire("./fixture/private", mocks, props);
  assert.equal(private.inc(), 101);
  });

method: _private_fn(name, [mock])

It allows you to get a reference to a private function:

  • name (Array|string): The path of the property to get.
it('should be able to get and invoke a private function', function() {
  var mockuire = require("mockuire")(module);
  var private = mockuire("./fixture/private");

  var ping = private._private_fn('ping');
  assert.equal(typeof ping, 'function');
  assert.equal(ping(), 'pong');


  var fn = private._private_fn(['data', 'someFn']);
  assert.equal(fn(), false);
});

You can also set a mock function:

function helloWorld() {
  return "Hello world!";
}

function alwaysTrue() {
  return true;
}

var mockuire = require("mockuire")(module);
var private = mockuire("./fixture/private");
var pingMocked = private._private_fn('ping', helloWorld);
var someFnMocked = private._private_fn(['data', 'someFn'], alwaysTrue);

it('mocked function should invoke the mock function', function() {
  assert.equal(pingMocked(), 'Hello world!');
  assert.equal(someFnMocked(), true);
});

it ('module\'s functions should invoke mock function', function() {
  assert.equal(private.testPing(), 'Hello world!');
  assert.equal(private.invoke(), true);
});

it('mocked function has a \'func\' property pointing to the original function', function() {
  assert.equal(pingMocked.func(), 'pong');
  assert.equal(someFnMocked.func(), false);
});

// mocked function has a method to reset to the original function.
pingMocked.reset();
someFnMocked.reset();

it('mocked function should be replaced by the original one.', function() {
  assert.equal(pingMocked(), 'pong');
  assert.equal(someFnMocked(), false);
});

it ('module\'s functions should invoke the original function', function() {
  assert.equal(private.testPing(), 'pong');
  assert.equal(private.invoke(), false);
});

Contrib - run tests

npm test

News

v2.1.0

  1. Support for setting and getting inner props added.
  2. Dependency to resolve module removed.
  3. Resolution of module's path improved.

v2.0.0

  1. Friendly with modules of code coverage, like istanbul.

Breaking changes

Changes for 2.x

  1. Does not accept compilers on constructor. The version 2.x relies on the infrastrucuture of nodejs in order to load and compile a module.

License

MIT License