Skip to content

Commit

Permalink
Provide feedback when t.is() values are deeply equal but not the same
Browse files Browse the repository at this point in the history
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 c41b2af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
27 changes: 19 additions & 8 deletions lib/assert.js
Expand Up @@ -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)]
}));
}
}
},

Expand Down
17 changes: 13 additions & 4 deletions test/assert.js
Expand Up @@ -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'));
Expand All @@ -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');
}, {
Expand Down

0 comments on commit c41b2af

Please sign in to comment.