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

mockeryjs

v1.0.1

Published

A simple interface for loading JSON testing mocks.

Downloads

7

Readme

MockeryJS

A simple interface for loading JSON testing mocks.

  • A mock is just a blob of JSON
  • A variant is a slight modification to a mock

Installing

npm install mockeryjs --save-dev

Synopsis

// make a mock
Mockery.mock('bill', {
    cost: 50
});

// some optional variant of the base mock
Mockery.variant('tip', function (bill) {
    bill.cost += 10;
    return bill;
});

// fetch the mock with optional variants applied
Mockery.ofA('bill').with('tip').fetch();

Registering Mocks

Mockery.mock('bill', {
    cost: 50,
    takeout: false,
    guests: 2
});

Registering Variants

A Variant is a set of one or more modifiers. There are two types of modifiers:

  1. Merge modifiers
  2. Function modifiers

Merge Modifiers

Merge modifiers are represented by Objects. They are merged into the base mock, overwriting base mock values if needed.

// will add the `over21` key to the `bill` mock
Mockery.variant('over21', {
    over21: true
});

Function Modifiers

Merge modifiers are represented by Functions. They are merged into the base mock, overwriting base mock values if needed.

// will add $10 to the cost of the `bill` mock
Mockery.variant('added-tip', function (bill) {
    bill.cost += 10;
    return bill;
});

Combo Modifiers

You can give a single variant multiple modifiers of any type by passing an array.

Mockery.variant('combo', [{
    over21: true
}, function () {
    bill.cost += 10;
    return bill;
}]);

Ownership and Duplicate Mocks

A variant can be 'owned' by a mock with .belongsTo(). If a variant is owned by a mock, then only that mock may use it to transform its data. If two variants have the same name, and one belongs to the given mock, then only the variant with the correct ownership will be applied.

Mockery.mock('bill',       { cost: 50 });
Mockery.mock('other-bill', { cost: 70 });

Mockery.variant('tax', { cost: 52 });
Mockery.variant('tax', { cost: 72 }).belongsTo('other-bill');

// will use the first (owner-less) tax variant.  ignores the other tax variant since it is owned by a different mock
Mockery.ofA('bill').with('tax').fetch();  // cost => 52

// will use the tax variant that belongs to it, since it takes precedence over the owner-less variant
Mockery.ofA('other-bill').with('tax').fetch();  // cost => 72

Fetching Mocks

To grab a base mock: (the following are all aliases)

Mockery.ofA(...);
Mockery.ofAn(...);
Mockery.ofThe(...);

To apply a variant to a base mock, use .with():

Mockery.ofA(...).with(...);

To complete a Mockery line and return the mocked data, use .fetch()

Mockery.ofA(...).with(...).fetch();
var baseMock = Mockery.ofA('bill').fetch();
console.log(baseMock);
/*
{
    cost: 50,
    takeout: false,
    guests: 2
}
 */

var modifiedMock = Mockery.ofA('bill').with('over21').with('added-tip').fetch();
console.log(modifiedMock);
/*
{
    cost: 60,
    takeout: false,
    guests: 2,
    over21: true
}
 */

Change Log

  • 1.0.1 - Fixing a syntax error and removing .file(), which was unsupported.
  • 1.0.0 - Initial Release