Saturday, November 07, 2015

Custom Matchers in Jasmine 2.3

Custom matchers, provided by the behavior-driven development framework Jasmine, is a great feature for testing JavaScript code. Custom Matchers helps to group multiple catch checking which makes the test cases clean and self explanatory.

Let’s look at an example that I tried recently

Here is a Calculator.js file:

   1: var Calculator = function () { };
   2:  
   3: Calculator.prototype.add = function (a, b) {
   4:     return a + b;
   5: };
   6:  
   7: Calculator.prototype.divide = function (a, b) {
   8:     return a / b;
   9: };

One of the test scenarios that I would like to try is to check whether the result of the method is in a particular range. The test method will look like this with existing matchers.



   1: describe('Calculator', function () {
   2:     var calc;
   3:  
   4:     beforeEach(function () {
   5:         calc = new Calculator();       
   6:     });
   7:  
   8:     it('should be able to add 1 and 1', function () {
   9:         expect(calc.add(1, 1)).toBe(2);
  10:     });
  11:  
  12:     it('should be able to divide 6 by 2', function () {
  13:         expect(calc.divide(6, 2)).toBe(3);
  14:     })
  15:  
  16:     it('should be able to divide a rational number', function () {
  17:         expect(calc.divide(1, 3)).toBeLessThan(0.34);
  18:         expect(calc.divide(1, 3)).toBeGreaterThan(0.3);        
  19:     })
  20: });

But you can replace the existing matchers with a custom matcher like this.



   1: describe('Calculator', function () {
   2:     var calc;
   3:  
   4:     beforeEach(function () {
   5:         calc = new Calculator();
   6:  
   7:         jasmine.addMatchers({
   8:             toBeBetween: function (util, customEqualityTesters) {
   9:                 return {
  10:                     compare: function (actual, a, b) {
  11:                         var result = {};
  12:                         result.pass = actual >= a && actual <= b;
  13:                         return result;
  14:                     }
  15:                 }
  16:             }
  17:         });
  18:     });
  19:  
  20:     it('should be able to add 1 and 1', function () {
  21:         expect(calc.add(1, 1)).toBe(2);
  22:     });
  23:  
  24:     it('should be able to divide 6 by 2', function () {
  25:         expect(calc.divide(6, 2)).toBe(3);
  26:     })
  27:  
  28:     it('should be able to divide a rational number', function () {
  29:         expect(calc.divide(1, 3)).toBeBetween(0.3, 0.34);
  30:     })
  31: });

You can reuse custom matchers to make the test cases clear and more simple.


What are some scenarios that you use custom matchers?