Skip to content

just-boris/jasmine-collection-matchers

Repository files navigation

jasmine-collection-matchers Build Status

A set matchers which helps compare collections

If you are using standard jasmine toEqual when comparing arrays, you may get the following error:

Message:
  Expected [ 1, 2, 3, 4, 5, 6 ] to equal [ 1, 2, 3, 4, 4, 6 ].

It can be hard to notice that fifth element is wrong. This module adds new matchers specially for collections that will print more detailed error messages about the values differences.

Message:
  The collections have equal length, but do not match.
    At 4: expected 5, actual 4

Much clearer!

Usage:

NPM

npm install jasmine-collection-matchers

Require into your tests and use:

require('jasmine-collection-matchers')

it('should match collection', function() {
    expect([1,2]).toHaveSameItems([1,2])
});

Bower

bower install jasmine-collection-matchers

Include combined matchers file:

<script src="/bower_components/jasmine-collection-matchers/index.js"></script>

Now you can use new matchers

it('should match collection', function() {
    expect([1,2]).toHaveSameItems([1,2])
});

Matchers

expect(<Array, Object>).toHaveSameItems(<Array, Object>, [<boolean> ignoreSort])

Validates that passed arrays or objects are identical. If not, prints the difference.

ignoreSort — ignore items order while comparing arrays. Default to false.

expect(<Array>).toHaveUniqueItems()

Validates that all items in array are unique. If not, prints indexes of duplicates

expect(<Array, String>).toHaveLength(<Number>)

Throws if actual does not have a length property

Validates the length vs. the expectation.