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-assertions-count

v1.0.2

Published

Plugin for ChaiJS allows checking how many assertions, asserts or expects were run per each test

Downloads

462

Readme

Chai Assertions Count

CI npm version Downloads License

Plugin for ChaiJS allows checking how many assertions or expects were run per each test.

Why do we need to check it?

Let's look at the test:

import InstanceGenerator from '../lib/instances-generator';

describe('suite #1', () => {
  it('test #1', () => {
    class S1T1A {
      /* Other props and methods are skipped */
      /**
       * I'm called after any instance of S1T1A is created
       */
      afterCreate(...args) {
        // I need to check `args` here
        // chai.expect(args)... 
      }
    }
    InstanceGenerator.create(S1T1A, 3); // create 3 instances of S1T1A
  });
});

Test looks pretty dummy, but its main idea that it's not possible to figure out was afterCreate called or not without dummy flag. It must be initialized on the top of the test. Then it must be toggle inside afterCreate and another expect must be added at the end of the test.

Tests becomes less readable.

Better way is to check how many expect were done.

Install

npm i -D chai-assertions-count

or

yarn add -D chai-assertions-count

Plugin

Use this plugin as you would all other Chai plugins.

const chai = require('chai');
const chaiAssertionsCount = require('chai-assertions-count');

chai.use(chaiAssertionsCount);

Usage

const chai = require('chai');
const chaiAssertionsCount = require('chai-assertions-count');
chai.use(chaiAssertionsCount);

describe('suite #2', () => {
  beforeEach(() => {
    chai.Assertion.resetAssertsCheck();
  });
  afterEach(() => {
    // you don't need both of them
    chai.Assertion.checkAssertionsCount();
    chai.Assertion.checkExpectsCount();
  });
});

Method resetAssertsCheck just drops internal counters and must be used before each test.

Method checkExpectsCount calculated how many times chai.expect was called. Use it in case when your tests use Expect style.

Method checkAssertionsCount calculated how many assertions were done. Main difference between this method and previous one is that single expect may do more than one assertion. Example below illustrates this:

const chai = require('chai');
const chaiAssertionsCount = require('chai-assertions-count');
chai.use(chaiAssertionsCount);

describe('suite #3', () => {
  it('test #1', () => {
    chai.Assertion.expectAssertions(3);
    chai.Assertion.expectExpects(2);

    chai.expect(1).to.be.equal(1);
    chai.expect([]).to.have.property('length', 0);
  });
});

Here are two expects and we "expect" that two of them will be executed. In the same time there are three assertions "under the hood". First expect has a single assertion. However, second expect has two of them. First one checks that property length exists and another one checks its value. So, be aware with expectAssertions counter.

Method expectExpects can cover most cases, so expectAssertions won't be used in 99.9%.

Restrictions

  • Works only with Expect style.
  • Stop other tests in the current suite on expectExpects or expectAssertions fail.