/* Code-Comments */

December 14, 2019

Chai Testing - Expect's Optional Parameter

When testing with the Chai assertion library, I like the expect syntax and how it reads.

The example from the docs demonstrates this nicely:

var expect = require('chai').expect
  , foo = 'bar'
  , beverages = { tea: [ 'chai', 'matcha', 'oolong' ] };


Reading the expect lines - it’s intuitive what you would expect to occur.

There is, however, an optional second parameter. This serves as a message that is prepended to the message in the event of an AssertionError.

Again, from the docs:

var answer = 43;

// AssertionError: expected 43 to equal 42.

// AssertionError: topic [answer]: expected 43 to equal 42.
expect(answer, 'topic [answer]').to.equal(42);

This is useful for situations where additional context behind an assertion is necessary, though it does break the readability of the assertion. In that sense, I’m happy to know about it, but will use it sparingly.

Stephen Weiss

Thanks for reading! My name's Stephen Weiss. I live in Chicago with my wife, Kate, and dog, Finn.
Click here to see the archives of my weeks in review and sign up yourself!