Skip to content

Comparing errors through deepEqual #3122

@rpepato

Description

@rpepato

I'm implementing tests for my js project and I need to compare an error returned from a rejected promise with an expected one.

It turns out that, no mater what is the content of the error object, assert.deepEqual always returns true as long as the two instances being compared are Errors.

The same behaviour does not occur when comparing strings, for example (node version 4.1.1):

node --version
v4.1.1

> let assert = require("assert");
undefined

> assert.deepEqual(new Error("a"), new Error("b"));
undefined

> assert.deepEqual(new String("a"), new String("b"));
AssertionError: [String: 'a'] deepEqual [String: 'b']
    at repl:1:8
    at REPLServer.defaultEval (repl.js:164:27)
    at bound (domain.js:250:14)
    at REPLServer.runBound [as eval] (domain.js:263:12)
    at REPLServer.<anonymous> (repl.js:393:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
    at REPLServer.Interface._line (readline.js:549:8)
    at REPLServer.Interface._ttyWrite (readline.js:826:14)

Reading the documentation for the deepEqual assertion, I didn't found any mentions regarding something special when comparing Errors.

Is this behaviour intended by design? Am I missing something important or should a consider another strategy to deeply compare two Error objects (instances + messages)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    assertIssues and PRs related to the assert subsystem.questionIssues that look for answers.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions