What is the difference between assert.deepStrictEqual and assert.deepEqual?

Viewed 12
const assert = require('node:assert')

...

it('should clone', () => {
  const input = { foo: 'bar' }
  const clone = structuredClone(input)
  assert.deepEqual(input, clone)
  assert.deepStrictEqual(input, clone)
})

assert.deepEqual passes without errors.

On assert.deepStrictEqual an error saying:

operator: 'deepStrictEqual'
message: 'Values have same structure but are not reference-equal:\n\n{\n  foo: 'bar'\n}\n'
name: 'AssertionError'

What does that error message mean?

What are the differences between these functions?

I am using node.js 18.8.0, jest 29.0.3.

1 Answers

deepEqual completes a deep equality comparison, using == to compare values that can not contain other values. deepStrictEqual, on the other hand, uses ===. A small difference is that using the strict variant +0 != -0.

It appears that the functions are behaving as expected, as calling the following code does not error:

const assert = require('node:assert');

const a = { foo: 'bar' };
const b = { foo: 'bar' };
assert.deepEqual(a, b);
assert.deepStrictEqual(a, b);

Instead the difference is probably caused by the implementation of the structuredClone function.

The error message is mostly nonsensical and is caused by you giving assert two objects that it knows are different but can't tell why.

Related