Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

chai says two identical arrays are not equal

ErrorAssertionError when two arrays with the same items are compared with equal

Short answer

Chai's equal is strict (===), so two arrays are equal only when they are the same object. Compare contents with eql or deep.equal. If order does not matter, use members. For arrays of objects use deep.members, because plain members compares items strictly.

  • Applies to Node.js 22, chai 5, mocha 11
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the smallest possible check. If this prints an assertion error, your test is using strict equality on arrays.

    node -e "const {expect}=require('chai'); expect([1,2]).to.equal([1,2])"
  2. 2Match what you see, then jump to its fix

    1. 1Both arrays print the same, yet the assertion failsequal compares by referenceFix
    2. 2eql fails because the arrays hold the same values in another orderThe items are the same but the order differsFix
    3. 3members fails for [{a: 1}] against [{a: 1}]Arrays of objects are compared strictly by membersFix

What does the error look like?

  • AssertionError when two arrays with the same items are compared with equal

Cause 1 of 3

equal compares by reference

1Check

The Chai docs say equal asserts strict (===) equality, so arrays and objects match only when they are the same reference.

grep -n "to.equal(" test/*.js

2Fix

Use eql, or deep.equal, to compare contents.

expect([1, 2]).to.eql([1, 2]);
expect([1, 2]).to.deep.equal([1, 2]);

3Verify

Run the test suite.

npx mocha

Confirmed by chaijs.com

Did this fix it?

Cause 2 of 3

The items are the same but the order differs

1Check

Deep equality on arrays is positional. The members assertion checks that the same items are present, in any order.

node -e "const {expect}=require('chai'); expect([1,2,3]).to.have.members([3,1,2]); console.log('ok')"

2Fix

Use members when order does not matter. Add include to accept a superset.

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

3Verify

Run the suite again.

npx mocha

Confirmed by chaijs.com

Did this fix it?

Cause 3 of 3

Arrays of objects are compared strictly by members

1Check

members compares items strictly by default, so two separate objects with equal content do not match.

grep -n "members(" test/*.js

2Fix

Add deep before members.

expect([{ a: 1 }]).to.have.deep.members([{ a: 1 }]);

3Verify

Run the suite.

npx mocha

Confirmed by chaijs.com

Did this fix it?

How do I stop it coming back?

  • Use equal only for primitives and references; use eql or deep.equal for arrays and objects.
  • Read the failure diff; Chai prints both arrays so mismatched order or types stand out.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.