# Mockery

> A simple interface for loading JSON testing mocks.

Latest version **1.0.0** (published 2016-10-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install Mockery
pnpm add Mockery
yarn add Mockery
bun add Mockery
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2016-10-26 |
| First published | 2016-10-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | mderoche |
| Maintainers | mderoche |

## Links

- npm: https://www.npmjs.com/package/Mockery
- Repository: https://github.com/mderoche/mockeryjs
- Homepage: https://github.com/mderoche/mockeryjs#readme
- Issues: https://github.com/mderoche/mockeryjs/issues
- npm.io page: https://npm.io/package/Mockery

## Dependencies (3)

- [clone](https://npm.io/package/clone.md) ^2.0.0
- [extend](https://npm.io/package/extend.md) ^3.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.16.4

## Recent versions

- 1.0.0 (latest) — 2016-10-26

## 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

## Synopsis

```javascript
// 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

```javascript
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.
```javascript
// 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.
```javascript
// 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.
```javascript
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.
```javascript
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
```javascript
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
}
 */
```

---
_Source: https://npm.io/package/Mockery · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
