# invariant.js

> Library for testing things you expect to always be satisfied

Latest version **0.0.2** (published 2014-12-08) · BSD-2-Clause license · 0 weekly downloads

## Install

```sh
npm install invariant.js
pnpm add invariant.js
yarn add invariant.js
bun add invariant.js
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2014-12-08 |
| First published | 2014-12-07 |
| Weekly downloads | 0 |
| License | BSD-2-Clause |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Andrej T. |
| Maintainers | whoeverest |
| Keywords | invariant, testing |

## Links

- npm: https://www.npmjs.com/package/invariant.js
- npm.io page: https://npm.io/package/invariant.js

## Dependencies (1)

- [colors](https://npm.io/package/colors.md) ~1.0.3

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [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

## Recent versions

- 0.0.2 (latest) — 2014-12-08
- 0.0.1 — 2014-12-08
- 0.0.0 — 2014-12-07

## README

## Invariant.js

Example:

```javascript
var test = require('./invariant.js');

function genPersonTests(person) {
    var nameLength = test.inv('name has low length', function() {
        return person.name.length < 16;
    });

    var surnameNoNumbers = test.inv('surname has no numbers', function() {
        return person.surname.search(/[0-9]/) === -1;
    });

    var isMinor = function() {
        return person.age < 18;
    };

    var idIsPositive = test.inv('id is positive', function() {
        return person.id > 0;
    });

    var mustNotDrink = test.inv('must not drink', function() {
        return person.canDrink === false;
    });

    var mustNotDrive = test.inv('must not drive', function() {
        return person.hasDriversLicence === false;
    });

    var minorInvariants = test.when(isMinor, test.all([mustNotDrive, mustNotDrink]));
    var tests = test.all([idIsPositive, nameLength, surnameNoNumbers, minorInvariants]);

    return tests;
}

var person = {
    name: 'Andrej',
    surname: 'Trajchevski',
    id: -2,
    age: 17,
    hasDriversLicence: true,
    canDrink: false
};

var personTests = genPersonTests(person);

console.log(personTests());
```

Prints:

```
Invariant "id is positive" not satisfied
Invariant "must not drive" not satisfied
false
```

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