Jasmine — Arguments and Equality

John Au-Yeung - Jan 23 '21 - - Dev Community

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

Testing is an important part of JavaScript.

In this article, we’ll look at how to check for equality in different ways.

Custom Argument Matchers

We can create our own custom matches to check for any kind of data we want.

For instance, we can write:

function multipleOf(number) {
  return {
    asymmetricMatch(compareTo) {
      return compareTo % number === 0;
    },

    jasmineToString() {
      return `<multipleOf: ${number}>`;
    }
  };
}

describe("multipleOf tests", function () {
  it('calls Buffer.alloc with some that is a multiple of 1024', function () {
    spyOn(Buffer, 'alloc').and.callThrough();
    Buffer.alloc(2048);
    expect(Buffer.alloc).toHaveBeenCalledWith(multipleOf(1024));
  });
});
Enter fullscreen mode Exit fullscreen mode

to create our matcher and our test that uses it.

The mutlipleOf function takes the number parameter, which is the argument we pass into the matcher.

In the function, we return an object with the asymmetricMatch and jasmineToString methods.

asymmetricMatch is what does the comparing.

We return true if the match is what we’re looking for and false otherwise.

jasmineToString lets us display the test name in a string.

Out matcher can be nested anywhere.

So we can write:

function multipleOf(number) {
  return {
    asymmetricMatch(compareTo) {
      return compareTo % number === 0;
    },

    jasmineToString() {
      return `<multipleOf: ${number}>`;
    }
  };
}

const foo = {
  bar() { }
}

describe("multipleOf tests", function () {
  it('calls foo.bar with some that is a multiple of 10', function () {
    spyOn(foo, 'bar');
    foo.bar({ name: 'mary', age: 20 });
    expect(foo.bar)
      .toHaveBeenCalledWith(
        {
          name: jasmine.any(String),
          age: multipleOf(10)
        }
      );
  });
});
Enter fullscreen mode Exit fullscreen mode

We create a spy for foo.bar to watch for the parameters.

We can use our matcher with multipleOf like we do with the jasmine.any matcher.

Custom Equality Testers

We can create ou own custom equality tester with Jasmine.

For instance, we can write:

function customTester(first, second) {
  if (typeof first === 'string' && typeof second === 'string') {
    return first[0] === second[1];
  }
}

describe("multipleOf tests", function () {
  beforeEach(function () {
    jasmine.addCustomEqualityTester(customTester);
  });

  it('is equal using a custom tester', function () {
    expect('abc').toEqual(' a ');
  });

  it('is not equal using a custom tester', function () {
    expect('abc').not.toEqual('abc');
  });

  it('works in nested equality tests', function () {
    expect(['abc', '123']).toEqual([' a ', ' 1 ']);
  });
});
Enter fullscreen mode Exit fullscreen mode

We create customTester that takes 2 parameters and check if 2 things are the same according to our criteria.

first is the value we’re checking and second is the value that we’re expecting.

We check if they’re both strings.

If they are, then we check if the first character of first is the same as the 2nd character of second .

It can check values no matter if they’re nested or not.

The last test does the check on each entry of the array.

Conclusion

We can check for matches in various ways.

We can check arguments and equality matches.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .