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 🙏

© 2026 – Pkg Stats / Ryan Hefner

mynock

v0.1.0

Published

Default settings for NPM package written in TypeScript

Readme

Mynock

Build Status

A simple wrapper to make stubbing / mocking / faking / spying on exported functions (including class constructors) a little easier.

Motivation

It is difficult to stub or replace a function exported from an ES6 module (e.g. export function or export default function). Stubbing libraries generally need both a reference to a parent object and the name of a child property that can be replaced. With ES6 exports, the parent object is the ES6 module itself. However, code external to the ES6 module have a read-only view of the module's exports.

Current solutions involve replacing a module's exports after transpiling it to CommonJS or bundling it with a tool like Webpack. While this works, it is not, strictly speaking, valid ES6 and makes your test setup much more complicated that it has to be.

mynock takes a simpler approach. It assumes that it is possible to wrap all exports used by code under test (or to wrap and rewrite the imports), and to just stub the wrappers instead.

Setup

npm install mynock --save

Note that mynock should be saved in your regular dependencies, not just your devDependencies, because it adds a thin wrapper to your non-test code as well.

mynock also makes use of the following environment variables:

  • process.env.NODE_ENV - Required. Set to 'production' to avoid unnecessary wrapping of your code in prod (which may affect performance).
  • process.env.MYNOCK_SYMBOL_CALLEE - Optional. Mynock returns a wrapped function with a __mynock_callee__ property pointing to the underlying function that should be called. It's this property that can be stubbed for test purposes. While unlikely, it's possible this string can conflict with existing properties on a function. If this environment variable is set, Mynock will use an ES6 Symbol instead (which by design should not conflict). However, many test frameworks and runtimes don't work completely as expected when using a Symbol to identify a function to be stubbed. Hence why we use a string by default.

Usage

In the exporting function, wrap the function being exported.

import wrap from 'mynock';

export default wrap(function myFunction(a, b) {
  return expensiveCall(a) + nonDeterministicCall(b);
});

In your test stub out the callee property of your function. callee is a variable exported by mynock, not the name of the property itself. In this example, we use Sinon + Tape, but mynock should work with just about any stubbing / faking library.

import { callee } from 'mynock';
import Sinon from 'sinon';
import test from 'tape';
import myFunction from './path/to/my-function';

test('my test', (t) => {
  const stub = Sinon.stub(myFunction, callee).callsFake((a, b) => a + b);
  t.equals(myFunction(1, 2), 3);
  Sinon.assert.calledWith(stub, 1, 2);
  t.end();
});

Mynock also works with classes (which are technically just constructor functions with prototypes) and static attributes assigned to classes or functions. For example, the following React example should work.

import wrap from 'mynock';
import React from 'react';
import PropTypes from 'prop-types';

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { prefix: '', suffix: '' };
  }

  render() {
    const { prefix, suffix } = this.state;
    const { content } = this.props;
    return (
      <div>
        { prefix }{ content }{ suffix }
      </div>
    );
  }
}

MyComponent.propTypes = {
  content: PropTypes.string.isRequired,
};
MyComponent.displayName = 'My Component';

export default wrap(MyComponent);

When stubbing classes, note that the constructor method is being replaced. In addition, the super function is not available in the stubbed context, so you will need to call the stubbed parent directly. For an example like the above, you can try something like this:

import { callee } from 'mynock';
import Sinon from 'sinon';
import { shallow } from 'enzyme';
import test from 'tape';
import MyComponent from './path/to/my-component';

test('my component', (t) => {
  const stub = Sinon.stub(MyComponent, callee)
    .callsFake(function constructor(props) {
      React.Component.call(this, props); // Replaces super(props) call
      this.state = {
        prefix: '¡',
        suffix: '!',
      };
    });

  const wrapper = shallow(<MyComponent content="hola" />);
  sinon.assert.calledWith(stub, { content: 'hola' });
  t.equal(wrapper.text(), '¡hola!', 'uses stubbed constructor');
  t.end();
});

To replace non-construtor methods, calling your library of choice's stub function on the class's prototype (e.g. stub(MyClass.prototype, 'methodName')).

Naming

By default, wrapping a function will return a new function with a different name (currently 'caller'). The wrap function takes a second argument you can use to control the name of the returned function.

import wrap from 'mynock';

function leonardo() { return 'swords'; }
leonard.name; // => 'leonardo'

const michaelangelo = wrap(leonardo);
michaelangelo.name; // => 'caller'

const donatello = wrap(leonardo, 'raphael');
donatello.name; // => 'raphael'