From ffb2522eb94afe46a8ccc23faebdcee23ea6f170 Mon Sep 17 00:00:00 2001 From: Kotaro Hirayama Date: Wed, 18 Apr 2018 17:59:17 +0900 Subject: [PATCH] [Breaking] ensure `Error` objects compare properly --- .eslintrc | 1 + index.js | 10 +++++++++- test/cmp.js | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.eslintrc b/.eslintrc index 4ff2c50..4fb86e1 100644 --- a/.eslintrc +++ b/.eslintrc @@ -16,6 +16,7 @@ "files": ["example/**", "test/**"], "rules": { "array-bracket-newline": 0, + "max-lines": 0, "max-params": 0, "max-statements": 0, "no-console": 0, diff --git a/index.js b/index.js index 61307f3..d980db2 100644 --- a/index.js +++ b/index.js @@ -50,7 +50,7 @@ function isBuffer(x) { } function objEquiv(a, b, opts) { - /* eslint max-statements: [2, 50] */ + /* eslint max-statements: [2, 70] */ var i, key; if (typeof a !== typeof b) { return false; } if (isUndefinedOrNull(a) || isUndefinedOrNull(b)) { return false; } @@ -60,6 +60,14 @@ function objEquiv(a, b, opts) { if (isArguments(a) !== isArguments(b)) { return false; } + // TODO: replace when a cross-realm brand check is available + var aIsError = a instanceof Error; + var bIsError = b instanceof Error; + if (aIsError !== bIsError) { return false; } + if (aIsError || bIsError) { + if (a.name !== b.name || a.message !== b.message) { return false; } + } + var aIsRegex = isRegex(a); var bIsRegex = isRegex(b); if (aIsRegex !== bIsRegex) { return false; } diff --git a/test/cmp.js b/test/cmp.js index b377743..538e827 100644 --- a/test/cmp.js +++ b/test/cmp.js @@ -342,3 +342,11 @@ test('functions', function (t) { t.end(); }); + +test('Errors', function (t) { + t.deepEqualTest(new Error('xyz'), new Error('xyz'), 'two errors of the same type with the same message', true, true, false); + t.deepEqualTest(new Error('xyz'), new TypeError('xyz'), 'two errors of different types with the same message', false, false); + t.deepEqualTest(new Error('xyz'), new Error('zyx'), 'two errors of the same type with a different message', false, false); + + t.end(); +});