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 mockMockery; // some optional variant of the base mockMockery; // fetch the mock with optional variants appliedMockery;
Registering Mocks
Mockery;
Registering Variants
A Variant is a set of one or more modifiers. There are two types of modifiers:
- Merge modifiers
- 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` mockMockery;
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` mockMockery;
Combo Modifiers
You can give a single variant multiple modifiers of any type by passing an array.
Mockery;
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;Mockery; Mockery;Mockery; // will use the first (owner-less) tax variant. ignores the other tax variant since it is owned by a different mockMockery; // cost => 52 // will use the tax variant that belongs to it, since it takes precedence over the owner-less variantMockery; // cost => 72
Fetching Mocks
var baseMock = Mockery;console;/*{ cost: 50, takeout: false, guests: 2} */ var modifiedMock = Mockery;console;/*{ cost: 60, takeout: false, guests: 2, over21: true} */