Powered by
This page is for an older version of Jasmine (2.2)
The current stable version of Jasmine is: 3.10 - You can also look at the docs for the next release: Edge


Custom Equality Testers

describe("custom equality", function() {

You can customize how jasmine determines if two objects are equal by defining your own custom equality testers. A custom equality tester is a function that takes two arguments.

  var myCustomEquality = function(first, second) {

If the custom equality tester knows how to compare the two items, it should return either true or false

    if (typeof first == "string" && typeof second == "string") {
      return first[0] == second[1];

Otherwise, it should return undefined, to tell jasmine's equality tester that it can't compare the items


Then you register your tester in a beforeEach so jasmine knows about it.

  beforeEach(function() {

Then when you do comparisons in a spec, custom equality testers will be checked first before the default equality logic.

  it("should be custom equal", function() {

If your custom tester returns false, no other equality checking will be done.

  it("should be custom not equal", function() {