Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rich Javascript Assertions Part 1: Chai

Rich Javascript Assertions Part 1: Chai

A quick description of some of the more specific assertions available in the Chai library.

Danielle Brook-Roberge

June 28, 2016
Tweet

More Decks by Danielle Brook-Roberge

Other Decks in Programming

Transcript

  1. Rich Assertions • Javascript testing libraries provide a wide variety

    of test assertions. • Beyond the “basic” assertions of truth, falseness, equality, and type, we have “rich” assertions which assert something more specific. • Rich assertions allow both clearer code and more explanatory error messages on failure. • This talk describes some rich assertions in Chai.
  2. assert.lengthOf • assert.equal(arr.length, len); becomes assert.lengthOf(arr, len); • On failure,

    this prints the array, as well as the actual and expected lengths.
  3. assert.include • assert.notEqual(str.indexOf(substr), -1); becomes assert.include(str, substr); • On failure,

    it shows the string and substring, rather than just stating that -1 is equal to -1. • This also works for arrays, as assert.include(arr, item);
  4. assert.match • assert.isTrue(/regex/.test(str)); becomes assert.match(str, /regex/); • On failure, it

    shows the string and regex, rather than just stating that false is not true.
  5. assert.property • assert.isTrue(‘prop’ in obj); becomes assert.property(obj, ‘prop’); • On

    failure, it shows the object contents and attempted property name.