Permalink
Browse files

Provide feedback when t.is() values are deeply equal but not the same

t.is() checks if values are exactly the same, and shows a diff otherwise. However if values are deeply equal the diff is misleading. Detect this and provide appropriate feedback to the user.

Fixes #1553
  • Loading branch information...
Lifeuser authored and novemberborn committed Oct 30, 2017
1 parent 1cd3a04 commit c41b2afc201118bfdc4d2039180ae2ddd0f697c9
Showing with 32 additions and 12 deletions.
  1. +19 −8 lib/assert.js
  2. +13 −4 test/assert.js
View
@@ -87,14 +87,25 @@ function wrapAssertions(callbacks) {
if (Object.is(actual, expected)) {
pass(this);
} else {
const actualDescriptor = concordance.describe(actual, concordanceOptions);
const expectedDescriptor = concordance.describe(expected, concordanceOptions);
fail(this, new AssertionError({
assertion: 'is',
message,
raw: {actual, expected},
values: [formatDescriptorDiff(actualDescriptor, expectedDescriptor)]
}));
const result = concordance.compare(actual, expected, concordanceOptions);
const actualDescriptor = result.actual || concordance.describe(actual, concordanceOptions);
const expectedDescriptor = result.expected || concordance.describe(expected, concordanceOptions);
if (result.pass) {
fail(this, new AssertionError({
assertion: 'is',
message,
raw: {actual, expected},
values: [formatDescriptorWithLabel('Values are deeply equal to each other, but they are not the same:', actualDescriptor)]
}));
} else {
fail(this, new AssertionError({
assertion: 'is',
message,
raw: {actual, expected},
values: [formatDescriptorDiff(actualDescriptor, expectedDescriptor)]
}));
}
}
},
View
@@ -213,10 +213,6 @@ test('.is()', t => {
assertions.is(undefined, false);
});
fails(t, () => {
assertions.is({foo: 'bar'}, {foo: 'bar'});
});
fails(t, () => {
// eslint-disable-next-line no-new-wrappers
assertions.is(new String('foo'), new String('foo'));
@@ -234,6 +230,19 @@ test('.is()', t => {
assertions.is('foo', NaN);
});
failsWith(t, () => {
assertions.is({foo: 'bar'}, {foo: 'bar'});
}, {
assertion: 'is',
message: '',
actual: {foo: 'bar'},
expected: {foo: 'bar'},
values: [{
label: `Values are deeply equal to each other, but they are not the same:`,
formatted: /foo/
}]
});
failsWith(t, () => {
assertions.is('foo', 'bar');
}, {

0 comments on commit c41b2af

Please sign in to comment.