chai testing - expect's optional parameter

2019-12-14

 | 

~1 min read

 | 

152 words

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' ] };

expect(foo).to.be.a('string');
expect(foo).to.equal('bar');
expect(foo).to.have.lengthOf(3);
expect(beverages).to.have.property('tea').with.lengthOf(3);

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.
expect(answer).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.


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!