Nice Philanthropist Metalhead
    Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

    Mockerypublic

    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

    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

    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
    }
     */

    Keywords

    none

    install

    npm i Mockery

    Downloadsweekly downloads

    5

    version

    1.0.0

    license

    MIT

    repository

    github.com

    last publish

    collaborators

    • avatar