# duck

> Rich matchers inspired by Hamcrest. Useful for generating helpful assertion failure messages in tests.

Latest version **0.1.12** (published 2021-05-11) · BSD license · 4.2M weekly downloads

## Install

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

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: high downloads; no vulnerabilities.

Warnings: no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.12 |
| Published | 2021-05-11 |
| First published | 2012-08-25 |
| Weekly downloads | 4.2M |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 19.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Michael Williamson |
| Maintainers | mwilliamson |
| Keywords | test, assert, matcher, assertThat |

## Links

- npm: https://www.npmjs.com/package/duck
- Repository: https://github.com/mwilliamson/duck.js
- Homepage: https://github.com/mwilliamson/duck.js#readme
- Issues: https://github.com/mwilliamson/duck.js/issues
- npm.io page: https://npm.io/package/duck

## Dependencies (1)

- [underscore](https://npm.io/package/underscore.md) ^1.13.1

## Alternatives

- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.1.12 (latest) — 2021-05-11
- 0.1.11 — 2013-04-13
- 0.1.10 — 2013-04-12
- 0.1.9 — 2012-09-08
- 0.1.8 — 2012-08-25

## README

# duck.js -- rich matchers with helpful messages on match failure

duck.js allows you to perform assertions on complex objects.
When those assertions fail, duck.js will try to produce helpful error messages.
For instance, suppose you want to assert the same property on an array of objects:

```javascript
var duck = require("duck");
var isArray = duck.isArray;
var hasProperties = duck.hasProperties;

var users = fetchUsers();
duck.assertThat(users, isArray([
    hasProperties({name: "Bob"}),
    hasProperties({name: "Jim"}),
]));
```

which might produce an error message like:

```
Expected [object with properties {
    name: 'Bob'
}, object with properties {
    name: 'Jim'
}]
but element at index 0 didn't match:
    value of property "name" didn't match:
        was 'Jim'
        expected 'Bob'
    expected object with properties {
        name: 'Bob'
    }
element at index 1 didn't match:
    value of property "name" didn't match:
        was 'Bob'
        expected 'Jim'
    expected object with properties {
        name: 'Jim'
    }
```

## API

The below is a quick reference to the API.
For more examples, take a look at the tests.

### duck.assertThat(value, matcher)

Assert that `value` satifies `matcher`.

If `value` satifies `matcher`, return normally, otherwise throw an
AssertionError describing the mismatch.

### duck.is(value)

If `value` is a matcher, return that matcher,
otherwise return `duck.equalTo(value)`.

### duck.equalTo(value)

Matcher for deep equality on `value`.

### duck.isObject(matcherObj)

An object `obj` matches `duck.isObject(matcherObj)` if:

* `obj` matches `duck.hasProperties(matcherObj)`, and
* there is no key that is present in `obj` but not in `matcherObj`

Sample usage:

```javascript
duck.isObject({
    name: "Bob",
    address: duck.isObject({
        city: "Cambridge",
        county: "UK"
    })
})
```

`duck.is` is called on each value of the matcher object, meaning that the
above is equivalent to:

```javascript
duck.isObject({
    name: duck.is("Bob"),
    address: duck.isObject({
        city: duck.is("Cambridge"),
        county: duck.is("UK")
    })
})
```

### duck.hasProperties(matcherProperties)

An object `obj` matches `duck.hasProperties(matcherProperties)` if,
for each `key` in `matcherProperties`, `matcherProperties[key].matches(obj[key])`

Sample usage:

```javascript
duck.hasProperties({
    name: "Bob",
    address: duck.hasProperties({
        city: "Cambridge",
        county: "UK"
    })
})
```

`duck.is` is called on each value of the matcher object, meaning that the
above is equivalent to:

```javascript
duck.hasProperties({
    name: duck.is("Bob"),
    address: duck.hasProperties({
        city: duck.is("Cambridge"),
        county: duck.is("UK")
    })
})
```

### duck.isArray(matcherArray)

An array `blah` matches `duck.isArray(matcherArray)` if:

* `blah.length == matcherArray.length`, and
* For `0 <= i < array.length`, `matcherArray[i].matches(blah[i])`

Sample usage:

```javascript
duck.isArray([
    duck.hasProperties({name: "Bob"}),
    duck.hasProperties({name: "Jim"}),
]))
```

`duck.is` is called on each element of the matcher array, meaning that the
following are equivalent:

```javascript
duck.isArray(["apple", "banana"])

duck.isArray([duck.is("apple"), duck.is("banana")])
```

### Matcher

Each matcher has the following methods:

#### matcher.matches(value)

Return `true` if `value` satifies this matcher, false otherwise.

#### matcher.describeMismatch(value)

Generate a string describing why `value` doesn't satisfy this matcher.
Behaviour is undefined if `value` actually satisifies the matcher.

#### matcher.matchesWithDescription(value)

Equivalent to:

```javascript
var isMatch = this.matches(value);
return {
    matches: isMatch,
    description: isMatch ? "" : this.describeMismatch(value)
};
```

Useful if you're likely to want both the boolean and the mismatch description.

#### matcher.describeSelf()

Generate a string describing the matcher.

## Thanks

Thanks to [Hamcrest](http://hamcrest.org/) for inspiration.

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