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

remock

v0.2.0

Published

Module mocking for Node.js unit testing

Downloads

14

Readme

remock

Module mocking for unit testing

Install

npm install remock

Why?

I wanted to develop unit tests for an application I was working on. I wanted to be able to:

  • Mock part of the global variable environment and have the global environment automatically restored after the unit test.
  • Bypass Node's module cache during a unit test.
  • Mock modules during a unit test.

Also, I wanted to work within these constraints:

  • The library should be more than async-friendly, it should be async-first.
  • The library should be dead simple; it shouldn't introduce its own vocabulary.
  • The library should only use public Node APIs and involve no stack-trace hackery.

I tried several different solutions and but wasn't happy with any of them.

Usage

/*

First, we'll get a mock function by calling remock with our local "require".

*/
'use strict';

const mock = require('remock')(require);

test('Testing 1, 2, 3', () => {

  /*

  We're going to mock the "request" module so that we don't hit the internet
  during our unit tests. Also, we're going to mock the Date.now function for
  the duration of the test.

  The mock function returns a Promise so it's compatible with Promise-aware
  testing libraries.

  The callback that we provide to the mock function can return a Promise, if
  it needs to do async stuff.

  */

  return mock({
    request: (url, callback) => { /* Mock */ },
  }, () => {
    Date.now = function() { return 1; };
    let comments = require('../src/comments');
    return comments.postComment({}).then(() => {
      // Make some assertions
    });
  });

});

API

remock(require)

Returns a mocking function which uses the require argument to resolve module names. You should pass the module-local require variable here.

mock([mockSpec, ] callback)

Mocks the module system using the provided mockSpec object and executes callback. The callback argument may return a promise. When that promise is resolved or rejected, the module system and global object are restored to their pre-mock state.

Returns a promise for the completion value of callback.

mock.start([mockSpec])

Instead of providing a callback to mock, you can instead call mock.start. This is useful when you want to start mocking from a test-framework hook, like before.

mock.stop()

If you used mock.start at the beginning of your test, you'll want to call mock.stop when you are all done. This can be called from a test-framework hook like after.

Mock Specifications

The mock specification argument provided to the mock function must be an object. The keys of the object are module paths and the values are the mocked module exports objects.

'use strict';

const assert = require('assert');
const mock = require('remock')(require);

mock({
  './local-module': {
    fn() { return 'local-module mock' },
  },
  pkg: function() {
    return 'pkg mock';
  },
}, () => {
  // Mock `Date.now`. Properties of the Date object will be restored after
  // this callback has completed.
  Date.now = () => 1;

  assert.equals(require('./local-module').fn(), 'local-module mock');
  assert.equals(require('pkg')(), 'pkg mock');
});

Keys with special semantics:

  • globalVars: An array of objects which will be restored to their pre-mock state when the promise returned by the callback has been resolved or rejected. The following global objects are always implicitly restored:
    • global
    • Date
    • Date.prototype