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

pretendr

v0.7.0

Published

Powerful JavaScript mocking

Downloads

8

Readme

pretendr

Powerful JavaScript mocking

travis build status

Install it

npm install pretendr.

Use it

var pretendr = require('pretendr');

Mock your objects. You can pass in real objects (pretendr(require("fs"))) but I prefer to keep mocks to a minimum so I know exactly what my code is doing.

var mockFs = pretendr({
    appendFile : function () {},
    createReadStream : function () {},
	readFile : function () {},
	readFileSync : function () {}
});

Each function creates a mock function and each object creates a mock object. (As a shortcut, pretendr() creates a standalone mock function which you can use as a dummy callback.)

mockFs now contains a mock property, which is what you pass in to your code for testing as a substitute for the real thing. This is virtually indistinguishable to your code from the object you are mocking.

var fs = mockFs.mock;
fs.readFile('f.txt', cb);
fs.readFileSync('f.txt');

It works well with injectr, which allows you to pass in your mocks when testing.

var myLib = injectr("../lib/mylib.js", {
    fs : mockFs.mock
});

Or you can use whichever dependency injection method you're used to.

Now let's monitor the calls:

assert.equal(fs.readFile.calls[0].args[1], 'f.txt');
assert.equal(fs.appendFile.calls.length, 0);

And run the callback, then test that it did what we expect:

fs.readFile.calls[0].callback();
assert.equal(fs.appendFile.calls.length, 1);

We can set return values:

mockFs.readFileSync.returnValue("some text");
// or
mockFs.readFileSync.fake(function () {
    // arguments and context are correctly passed to this function
    return "some text";
});

Templates allow you to create a new pretendr object each time the function is run:

mockFs.createReadStream.template({
    on : function () {}
});

Then retrieve your created pretendr:

var mockRs = mockFs.createReadStream.calls[0].pretendr;
assert.equal(mockRs.calls[0].args[0], "data");

If you have lots of function calls and you only want to test one of them, use findCall to find a call by it's arguments:

mockFs.findCall(['somefile.txt']).callback();

findCall can also take a number for a number of arguments, or a function which should return true for each matching argument.

Share it

pretendr is under the MIT License. Fork it. Modify it. Pass it around.