📜 ⬆️ ⬇️

Creating a custom matcher for unit testing in Jasmine 2.0

Recently faced with the need to write a custom matcher in jasmine. The first thing I started to google and found an example where everything is clearly and clearly explained. The code itself is shown below:

describe('Hello world', function () { beforeEach(function() { this.addMatchers({ toBeWithinOf: function(distance, base) { this.message = function() { var lower = base - distance, upper = base + distance; return "Expected " + this.actual + " to be between " + lower + " and " + upper + " (inclusive)"; }; return Math.abs(this.actual - base) <= distance; } }); }); it('should be within in range', function () { expect(8).toBeWithinOf(2, 6); }); }); 


In this example, a matcher is created that will tell whether the entered number ( this.actual ) is in a certain range. Note that when you call a matcher, two parameters are passed: base - the number from which a certain value will be counted in both directions - distance , eventually forming a search interval - [base - distance, base + distance] . In this case, we expect that 8 will be in the interval [4, 8]. If the number specified by the user is outside the interval, then in the tests we will see an error that we form with the help of the function this.message .

Everything seems to be nothing, but when you run the example in jasmine 2.0 none of this works. It turns out that the syntax has changed a bit.
')
I tried a lot of ways and went through more than one forum I was able to resolve this issue. The solution code is as follows:

 describe('Hello world', function () { beforeEach(function () { jasmine.addMatchers({ toBeWithinOf: function () { return { compare: function (actual, distance, base) { var lower = base - distance, upper = base + distance, result = { pass: Math.abs(actual - base) <= distance }; if(!result.pass) { result.message = "Expected " + actual + " to be between " + lower + " and " + upper + " (inclusive)"; } return result; } } } }) }); it('should be within in range', function () { expect(8).toBeWithinOf(2, 6); }); }); 

As you can see, the method for calling the add match function has been changed - ( this.addMatchers -> jasmine.addMatchers ). Also, from the function itself, we are forced to return an object in which the compare method must be declared, which takes all the parameters, and not the matcher function itself.

Please note that the first parameter ( actual ) corresponds to the number we want to check, and if we need to transfer our parameters to the matcher itself (as in our case, base and distance), we can indicate them in the same place.

Inside the compare function itself, we create an object into which we immediately write the comparison property ( pass ). And only if this property does not work out as a result, then only then the message property is created, in which you can form a text that the user will see if the test fails.

A link to a working example can be found here - fiddle .

That's all.

Source: https://habr.com/ru/post/253323/


All Articles