question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asserting that assertions were made

See original GitHub issue

Is there a way to assert whether any chai assertions have been made?

We’ve been bitten a number of times by tests passing gloriously in Mocha, only to realize that it’s because no assertions were made for whatever reason (a common one is the misspelling of assertions, such as calledwith instead of calledWith. This would easily be caught by having a test hook that runs after each test to make sure that assertions were indeed made. It won’t help with situations where some assertions are made and others are broken by misspelling or such, but in our experience it’s been less common (if occurring at all.)

If not possible, I’m happy to contribute, just let me know!

Issue Analytics

  • State:closed
  • Created 11 years ago
  • Reactions:1
  • Comments:35 (12 by maintainers)

github_iconTop GitHub Comments

14reactions
sevillaarvincommented, Apr 10, 2019

Hi, jest has an expect.assertions method that verifies that a certain number of assertions are called during a test. I was hoping there was a chai equivalent to this. I’ve been searching in google and couldn’t find anything so far.

3reactions
martin-gcommented, Mar 27, 2019

It seems this feature was not actually implemented. At least I didn’t find anything neither in the documentation nor in the tests showing how to do it. In my application we use the assert interface, so here is how I implemented it:

in src/test/util/chai-extensions.js:

'use strict';

/**
 * An extension/plugin for ChaiJS that collects the number of executed assertions and
 * compares them against the expected ones in MochaJS's afterEach hook
 */
module.exports = {

  /**
   * Overrides Chai's 'assert' method to intercept any calls to it and to count them.
   *
   * 'this' is chai['test name'] to isolate the extra added properties from overwriting
   * in concurrently executed tests.
   *
   * @param _chai The instance of Chai
   * @param utils The Chai utilities used to override the 'assert' method
   */
  assertionCountingPlugin: function (_chai, utils) {
    const self = this;
    self.expectedAssertions = 0;
    self.countedAssertions = 0;
    self.expect = function(num) {
      self.expectedAssertions = num;
    };

    utils.overwriteMethod(_chai.Assertion.prototype, 'assert', function (_super) {
      return function () {
        self.countedAssertions++;
        _super.apply(this, arguments);
      };
    });
  },

  /**
   * MochaJS hook that is used to verify that the number of expected assertions matches the number
   * of executed ones
   */
  afterEach: function() {
    if (this.expectedAssertions !== 0 && this.expectedAssertions !== this.countedAssertions) {
      throw new Error('Expected ' + this.expectedAssertions + ' but ' + this.countedAssertions + ' have been recorded!');
    }
    // reset
    this.expectedAssertions = 0;
    this.countedAssertions = 0;
  }
};

In the tests:

const chai = require('chai');
const chaiExtensions = require('./util/chai-extensions');
const assert = chai.assert;

const SpecName = 'MySpecialTest';
const chaiContext = chai[SpecName] = {};
chai.use(chaiExtensions.assertionCountingPlugin.bind(chaiContext));
afterEach(chaiExtensions.afterEach.bind(chaiContext));

describe(SpecName, function() {

  it('testing', async () => {
    chaiContext.expect(3);

    assert.equal(1, 1);
    assert.ok('truthy');
    assert.isFalse(false);
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Assertion - Stanford Encyclopedia of Philosophy
We make assertions to share information, coordinate our actions, defend arguments, and communicate our beliefs and desires.
Read more >
Programming With Assertions - Oracle Help Center
An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if...
Read more >
Python's assert: Debug and Test Your Code Like a Pro
Assertions are a convenient tool for documenting, debugging, and testing code during development. Once you've debugged and tested your code with ...
Read more >
How can you assert an assertion was done - Stack Overflow
We catch the error and assertTrue(true) to state that it was ok. But we can'T assert that the assertion passed in any reasonnable...
Read more >
Assertions | Cypress Documentation
Cypress bundles the popular Chai assertion library, as well as helpful extensions ... These chainers are available for BDD assertions ( expect /...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found