2

I have encountered a weird error when trying to assert that a result should equal to an array, when using Chai.

Code Example

   describe("compare array", function() {
        it("should return an empty array", function() {
            const result = getEmptyList(); // just a silly example
            expect(result).to.equal([]);
        });
   });

Result

  × compare array
    PhantomJS 2.1.1 (Windows 8.0.0)
  expected [ find: [Function] ] to equal [ find: [Function] ]
  AssertionError@C:/Code/example/node_modules/chai/chai.js:9449:24
  assert@C:/Code/example/node_modules/chai/chai.js:239:31
  assertEqual@C:/Code/example/node_modules/chai/chai.js:1387:18
  methodWrapper@C:/Code/example/node_modules/chai/chai.js:7824:30
  test/unit/utils/example.js:5:32

When I instead expect the length of the result to equal zero, it works fine. Can anyone share some insights to why this is happening. Why is [ find: [Function] ] all of a sudden expected, instead of [] as was desired?

I am using Karma as my test runner.

ProgrammerPer
  • 1,125
  • 1
  • 11
  • 26

1 Answers1

4

Since one array is never === to another, for arrays you need the deep qualifier for equals:

expect(result).to.deep.equal([]);
// --------------^^^^^

From the docs:

Causes all .equal, .include, .members, .keys, and .property assertions that follow in the chain to use deep equality instead of strict (===) equality.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875