Skip to content

assert.deepEqual assertion error depth limit #15696

Closed
@caub

Description

@caub

I think it would be better if the message of assert.deepEqual was not limited to a depth of 3, and could display the first difference between the objects, like some testing libraries do

const assert = require('assert');

assert.deepEqual({
	foo: {
		bar: {
			qux: {
				lolcat: {
					bip: {
						yup: 56
					}
				}
			}
		}
	}
}, {
	foo: {
		bar: {
			qux: {
				lolcat: {
					bip: {
						yup: 56.3
					}
				}
			}
		}
	}
})

// outputs:
// AssertionError [ERR_ASSERTION]: { foo: { bar: { qux: [Object] } } } deepEqual { foo: { bar: { qux: [Object] } } }

A simple implementation example:

const deepEq = (o1, o2) => {
	const keys1 = Object.keys(o1);
	const keys2 = Object.keys(o2);
	if (keys1.length !== keys2.length) {
		assert.deepEqual(o1, o2);
	}
	for (let i=0; i<keys1.length; i++) {
		const v1 = o1[keys1[i]], v2 = o2[keys1[i]];
		if (v1 && typeof v1 === 'object') {
			deepEq(v1, v2);
		} else {
			assert.equal(v1, v2);
		}
	}
};

The current implementation passes the entire objects in the Error, and .toString is limited in 3 in depth I guess

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.feature requestIssues that request new features to be added to Node.js.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions