Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha/Chai testing expected vs actual arrays of objects

What's the best way to assert that the expected results matches the actual results when both are arrays of objects? My immediate thought was to use Array prototype filter and check that the intersection is the same size as expected, i.e.:

describe('select',function(){
  it("should return selected columns", function(done) {
    var query = "select lunchTime, name";

    var actual = ... results of the query, an array of anonymous objects ...

    // expected results
    var expected = [{"lunchTime": "12:00:00", "name": "John"}, 
                   {"lunchTime": "12:00:00", "name": "Dave"}, 
                   {"lunchTime": "13:00:00", "name": "Sally"}, 
                   {"lunchTime": "12:00:00", "name": "Ben"}, 
                   {"lunchTime": "12:00:00", "name": "Dana"}, 
                   {"lunchTime": "13:00:00", "name": "Mike"}];

    var intersection = actual.filter(function(n) {
        return expected.indexOf(n) != -1
    });

    expect(intersection).to.have.length(expected.length);
    expect(actual).to.have.length(expected.length);
  });
});

Does this approach make sense? Is there a better way to assert the query results match expectations?

like image 552
Pauli Price Avatar asked Jan 01 '14 00:01

Pauli Price


People also ask

How do I compare two arrays in Chai?

members becomes more obvious when comparing arrays of objects. Mentioned before, eql is an equality assertion in Chai. js which will perform a deep equal instead of a strict equal. An third way to compare two arrays of primitive values is to use the flagging property deep.

What is mocha chai testing?

Mocha is a JavaScript test framework running on Node. js and in the browser. Mocha allows asynchronous testing, test coverage reports, and use of any assertion library. Chai is a BDD / TDD assertion library for NodeJS and the browser that can be delightfully paired with any javascript testing framework.

What is chai used for in testing?

Chai is an assertion library that is mostly used alongside Mocha. It can be used both as a BDD / TDD assertion library for NodeJS and can be paired with any JavaScript testing framework. It has several interfaces that a developer can choose from and looks much like writing tests in English sentences.


2 Answers

As you noted, for sets, you can use:

expect(actual).to.have.members(expected);

This will fail if there are any members in expected that are not in actual, and vice versa.

You can also do:

expect(actual).to.contain.members(expected);

Which will fail if there are members in expected that are not in actual, but will not fail if there are members in actual that are not in expected. (In other words, actual must be a superset of expected).

If you're comparing objects, you probably want to deep compare:

expect(actual).to.deep.have.same.members(expected);

e.g.:

expect([{a:'a'}, {a:'b'}]).to.deep.have.same.members([{a:'a'}, {a:'b'}]);

(edited: to.have.same.members() is actually the same as to.have.members().)

like image 170
Jason Walton Avatar answered Oct 12 '22 16:10

Jason Walton


Chai has a members matcher that does what I need:

expect(actual).to.have.members(expected);
like image 33
Pauli Price Avatar answered Oct 12 '22 14:10

Pauli Price