npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chai-change

v2.1.2

Published

Assert that a change you expected to happen, happened, with the chai library

Downloads

16,608

Readme

Chai Change

Build Status

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.create();
expect(users.count()).to.equal(1);

instead assert that the action actually causes the expected change

expect(() => {
  users.create();
}).to.alter(users.count, { by: 1 });

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.

<script src="chai-change.js"></script>

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 = require('chai');

chai.use(require('chai-change'));

Expect API

.change

Asserts that the value returned by function passed to change() changes after the function has run:

let x = 0;

expect(() => { x += 1; }).to.alter(() => x);
expect(() => {         }).not.to.alter(() => 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.

expect(() => { x += 1 }).to.alter(() => x, { by: 1 });
expect(() => { x += 1 }).to.alter(() => x, { from: x });
expect(() => { x += 1 }).to.alter(() => x, { from: x, to: x + 1 });
expect(() => { x += 1 }).to.alter(() => x, { to: x + 1 });

Assert API

assert.alters

Asserts that the value returned by changeWatcher changes after the changer function has run:

let x = 0;
assert.alters(changer, changeWatcher);

function changer() { x += 1; }
function changeWatcher() { 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.alters(() => { x += 1 }, () => x, { by: 1 });
assert.alters(() => { x += 1 }, () => x, { from: x });
assert.alters(() => { x += 1 }, () => x, { from: x, to: x + 1 });
assert.alters(() => { x += 1 }, () => x, { to: x + 1 });

assert.unaltered

Asserts that the value returned by changeWatcher doesn't change after the changer has run:

let x = 0;
const noop = () => undefined;
assert.unaltered(noop, () => x);

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.


it("creates a user", () => {
  let count = 0;
  const User = {
    create(attrs) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          count += 1
          resolve();
        });
      });
    },
    count() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(count);
        });
      });
    },
  };

  // when `changer` or `changeWatcher` returns a promise the expectation will return a promise as well
  return expect(() => (
    User.create({name: "bob"});
  )).to.alter(() => (
    User.count();
  ),{
    by: 1,
  });
})

With error-first callback

let count = 0;
const User = {
  create(attrs,cb) {
    setTimeout(() => {
      count += 1
      cb();
    });
  },
  count(cb) {
    setTimeout(() => {
      cb(null,count);
    });
  },
};

expect((stepDone) => {
  User.create({name: "bob"}, stepDone)
}).to.alter((stepDone) => {
  User.count(stepDone);
},{
  by: 1,
  callback: done
});

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 to alter to avoid the .change method added to chai in [email protected].