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 🙏

© 2025 – Pkg Stats / Ryan Hefner

xerox

v0.2.0

Published

Xerox is a unit testing mock framework.

Downloads

7

Readme

Xerox

Xerox is a unit testing mock framework.

Usage

Xerox is best used with a dependency manager like proxyquire. To use, simply include in your test file and create a mocked class.

// mymock.js
var Xerox = require('xerox');

var mock = new Xerox('MyMock');

module.exports = {
	mock: function(test, method) {
		return mock.copy(test, method);
	},
	proxy: new Xerox.documents.MyMock
};

Then use proxyquire to inject MyMock as a dependency in your test file.

// mytest.js
var proxyquire = require('proxyquire');
var mymock = require('./mymock.js');

var mymodule = proxyquire('./mymodule.js', {
	dependency: mymock.proxy
});
...

Then use mymock.mock function to mock a specific function exported by the mocked module.

...
function test(expected, actual, message) {
	...
}
var myfunc = mymock.mock(test, 'myfunc');

myfunc.expects(1, 'two').yields('3');
mymodule.run(1, 'two');

Where the module under test may look like the following:

// mymodule.js
var Adder = require('dependency');

var adder = new Adder();

module.exports = {
	run: function(a, b) {
		var value = adder.myfunc(a, b);
		console.log(value);
	}
};

Documentation

Xerox

Class that manages a mock document.

Xerox

Xerox Document constructor.

Parameters:

  • name <String>: Name of the class to mock.

Xerox#copy(test, method)

Creates new template to mock on the provided Document with a given name.

Parameters:

  • test <Function, null>: callback function to test values.
    • expected: expected value
    • actual: actual value
    • msg: optional message to provide
  • method <String>: method name to be added to the prototype.
    • If method is '@construct' then the constructor for this class is mocked.

Returns:

  • <Template>: Template to set mock parameters.

Template

Instances of Template are returned by Xerox#copy.

Template#expects([arg1, [arg2... [argN]]])

Sets the arguments the next invocation of the method will expect.

Parameters:

  • arg: Value to expect in next invocation. Will be used with test.

Returns:

  • <Template>: Template it was invoked on to allow chaining.

Template#throws(err)

Sets err that the next invocation will throw after parameters have been verified.

Parameters:

  • err: The error to be thrown.

Returns:

  • <Template>: Template it was invoked on to allow chaining.

Template#sets(property, value)

Sets a property with the given name and value to the object this object was invoked on.

Parameters:

  • property: The property name to be set.
  • value: The value for the property to be set.

Returns:

  • <Template>: Template it was invoked on to allow chaining.

Template#yields(value)

Sets value that will be returned from next invocation.

Parameters:

  • value: The value to be returned.

Returns:

  • <Template>: Template it was invoked on to allow chaining.

Template#callback(err, value)

Sets callback parameters that will be called from next invocation. If the last parameter of the mocked function is a function then it is used as a callback with the given parameters.

Parameters:

  • err: The error parameter to be used with the next callback.
  • value: The value to be used with the callback.

Returns:

  • <Template>: Template it was invoked on to allow chaining.

Template#calls(method)

Adds a function to be called to a queue. Each method will be called when the mocked function is invoked.

Parameters:

  • method <Function>: The function to be called on invocation.

Returns:

  • <Template>: Template it was invoked on to allow chaining.

Template#then()

Returns a new instance of the invocation to test. This instance will be used on the invocation after the previous instance.

Returns:

  • <Template>: A new template to modify. Allows chaining.

License

MIT