chai says two identical arrays are not equal
AssertionError when two arrays with the same items are compared with equalShort 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.
Fix it fast
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])"2Match what you see, then jump to its fix
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/*.js2Fix
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 mochaConfirmed by chaijs.com
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 mochaConfirmed by chaijs.com
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/*.js2Fix
Add deep before members.
expect([{ a: 1 }]).to.have.deep.members([{ a: 1 }]);3Verify
Run the suite.
npx mochaConfirmed by chaijs.com
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix