I am testing a function in a node.js program, which should save some data to a mongo database. I am using mocha, chai and should.
In the schema, I have defined the data to be saved as
data: [{type: Number, min: 0}]
The test saves the array [1,2,3,4,5,6]
to the database, then find
s it again, and pares what it found with the array that was saved. The test is
result.data.should.eql([1,2,3,4,5,6]);
The test result is
Uncaught AssertionError: expected [1,2,3,4,5,6]
to deeply equal [ 1, 2, 3, 4, 5, 6 ]
According to this chai.js example, paring arrays this way should work just fine.
When I test the result with result.data.should.deep.include.members([1,2,3,4,5,6]);
, the test passes just as expected.
Can anyone explain to me why this is not working?
I am testing a function in a node.js program, which should save some data to a mongo database. I am using mocha, chai and should.
In the schema, I have defined the data to be saved as
data: [{type: Number, min: 0}]
The test saves the array [1,2,3,4,5,6]
to the database, then find
s it again, and pares what it found with the array that was saved. The test is
result.data.should.eql([1,2,3,4,5,6]);
The test result is
Uncaught AssertionError: expected [1,2,3,4,5,6]
to deeply equal [ 1, 2, 3, 4, 5, 6 ]
According to this chai.js example, paring arrays this way should work just fine.
When I test the result with result.data.should.deep.include.members([1,2,3,4,5,6]);
, the test passes just as expected.
Can anyone explain to me why this is not working?
Share Improve this question asked Nov 20, 2014 at 8:30 larlonlarlon 5871 gold badge6 silver badges17 bronze badges1 Answer
Reset to default 9Mongoose decorates Arrays with a lot of its own methods. You won't see these when you do console.log(result.data)
, but deep-eql (the library that Chai uses to do deep equality) will consider them when doing parison. This is why the test fails when you use .eql()
: you are paring a special Mongoose Array to a plain vanilla Array.
Rather, if you did
result.data.toJSON().should.eql([1,2,3,4,5,6]);
your test would pass as you expect.
The .members()
assertion goes through a different code path, which is why it happens to work.
As an aside, the example that worked for you doesn't do exactly what you want, it is a superset test. What you actually want is:
result.data.should.have.members([1,2,3,4,5,6]);