Assertions

Reporting Ability (Error Messaging)

Assertions provide no feedback unless the test fails. It is because of this that well displayed and useful error messaging becomes important. Based on the assertion modules mentioned before, here is a report on the capability of each as far as effective reporting is concerned.

Assert

The Assert interface supports custom messages in all of its methods.

var assert = require('assert');
var result = 1+2;
assert (result == 4,'one plus two is three');
assert.js:86
  throw new assert.AssertionError({
        ^
AssertionError: one plus two is three
    at Object. (/Users/QualityWorks/Documents/AssertionPractise/assertionPractise.js:3:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Should/Expect/Chai

Custom messages are supported in some methods in each of these interfaces.

Should

var should = require('should'); 
var company = {
    name: 'QWQ'
  , location: ['LA', 'Jamaica']
};
company.should.have.property('name').with.lengthOf(2);
throw new AssertionError(params);
          ^
AssertionError: expected 'QWQ' to have property length of 2 (got 3)
    at Assertion.fail (/Users/QualityWorks/Documents/AssertionPractise/node_modules/should/lib/assertion.js:180:17)
    at Assertion.prop.value (/Users/QualityWorks/Documents/AssertionPractise/node_modules/should/lib/assertion.js:65:17)
    at Object. (/Users/QualityWorks/Documents/AssertionPractise/assertionPractise.js:12:43)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Expect

var expect = require('expect');
var name = 8;
expect(name).toBeA('string'); 
 throw new Error(messageFormat.replace(/%s/g, function () {
        ^
Error: Expected 8 to be a 'string'
    at Object.assert [as default] (/Users/QualityWorks/Documents/AssertionPractise/node_modules/expect/lib/assert.js:20:9)
    at Expectation.toBeA (/Users/QualityWorks/Documents/AssertionPractise/node_modules/expect/lib/Expectation.js:106:24)
    at Object. (/Users/QualityWorks/Documents/AssertionPractise/assertionPractise.js:24:14)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Chai

var expect= require('chai').expect;
var name =8;
expect(name).to.be.a('string');// THIS IS THE SAME EXAMPLE FROM ABOVE BUT NOTICE "toBeA" is replaced by "to.be.a."
 throw new AssertionError(msg, {
            ^
AssertionError: expected 8 to be a string
    at Object. (/Users/QualityWorks/Documents/AssertionPractise/assertionPractise.js:28:20)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Must

The must interface supports autogenerated error messages which are customizable.

var must = require("must/register");
var sum =1+43;
sum.must.equal(45,);
throw new AssertionError(msg, opts)
        ^
AssertionError: 44 must equal 45
    at Object. (/Users/QualityWorks/Documents/AssertionPractise/assertionPractise.js:17:10)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3


Shantel Stewart

Suggested Articles