Chai Change
Assert that a change you expected to happen, happened, with this plugin for the chai assertion library. The plugin works in node and the browser, asynchronously or synchronously.
The idea of the plugin is to make your tests more robust. Rather than doing:
users;to;
instead assert that the action actually causes the expected change
to;
This is more robust as it avoids false positives: in this example, if users.count()
was already 1 and users.create()
was not implemented, the first example would still pass. Using the change expectation, since there was not a change {by: 1}
from the starting value, the test would correctly fail.
Installation
Node.js
chai-change
is available on npm.
$ npm install chai-change
Browser
Either install via npm, or download chai-change
and save as chai-change.js
. Then simply include after chai.js
.
Plug In
If you are using chai-change
in the browser, there is nothing you need to do.
If you are using node, you just need to tell chai
about the plugin:
const chai = ; chai;
Expect API
.change
Asserts that the value returned by function passed to change()
changes after the function has run:
let x = 0; to;notto;
You can pass options to be specific about the changes expected. Use the from
key to enforce a starting value, a to
key for and ending value, and a
by
key to enforce a numeric change.
to;to;to;to;
Assert API
assert.alters
Asserts that the value returned by changeWatcher
changes after the changer
function has run:
let x = 0;assert; { x += 1; } { return x }
You can pass options to be specific about the changes expected. Use the from
key to enforce a starting value, a to
key for and ending value, and a
by
key to enforce a numeric change.
assert;assert;assert;assert;
assert.unaltered
Asserts that the value returned by changeWatcher
doesn't change after the changer
has run:
let x = 0;const noop = undefined;assert;
Asynchronous asserts
Both the changer
and changeWatcher
callbacks can return a promise, or take a node-style callback, with error
as the first parameter. If you provide a callback you need to give a final callback:
option to the change assertion, that is used to notify your test runner that the test is complete.
With promises
Many test runners - for instance mocha - support simply returning promises from it()
or test()
blocks to support asynchronous tsts. chai-change supports this style.
If your runner doesn't support returning promises, you can use the .then()
method to call a callback based API etc (or use callback:
as in the error-first callback docs below.
With error-first callback
let count = 0;const User = { ; } { ; }; to;
Tests
Node: npm install && npm test
.
Browser: npm install
then open test/index.html
.
Changelog
2.1
Promise support - thanks to @talyssonoc!
Both the changeWatcher
and changer
functions can now return promises. The expectation also returns a promise when used with promises, which can be used directly with mocha etc.
2.0
- BREAKING CHANGE Change whole API from
change
toalter
to avoid the.change
method added to chai inchai@2.0.0
.