Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unexpected messy missing diff #547

Merged
merged 2 commits into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ module.exports = expect => {
);

expect.addAssertion(
'<any|Error|object> to [exhaustively] satisfy <expect.it>',
'<any|Error> to [exhaustively] satisfy <expect.it>',
(expect, subject, value) => expect.promise(() => value(subject))
);

Expand Down Expand Up @@ -1410,7 +1410,7 @@ module.exports = expect => {
);

expect.addAssertion(
'<any> to [exhaustively] satisfy [assertion] <expect.it>',
'<any|object> to [exhaustively] satisfy [assertion] <expect.it>',
(expect, subject, value) =>
expect.withError(
() => value(subject, expect.context),
Expand Down
61 changes: 61 additions & 0 deletions test/assertions/to-satisfy.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,67 @@ describe('to satisfy assertion', () => {
});
});

describe('with an expect.it and customised types', () => {
function Foo(thing) {
this.thing = thing;
}
const clonedExpect = expect.clone();

clonedExpect.addType({
name: 'Foo',
identify(obj) {
return obj instanceof Foo;
},
inspect(obj, depth, output) {
output.text('Foo', 'green');
}
});

clonedExpect.addAssertion(
'<Foo> to satisfy <object>',
(expect, subject, value) => {
return expect.withError(
() => expect(subject.thing, 'to satisfy', value.thing),
err => {
expect.fail({
diff: (output, diff, inspect, equal) => {
output.text('-- custom diff --', 'yellow').nl(2);

const thingDiff = err.getDiff(output);
if (thingDiff) {
output.append(thingDiff);
}

return output;
}
});
}
);
}
);

it('should handle async diffs with nested parts', () =>
expect(
() =>
clonedExpect(new Foo({ foo: 123 }), 'to satisfy', {
thing: expect.it('when delayed a little bit', 'to equal', {
foo: 787
})
}),
'to error',
'expected Foo\n' +
"to satisfy { thing: expect.it('when delayed a little bit', 'to equal', { foo: 787 }) }\n" +
'\n' +
'-- custom diff --\n' +
'\n' +
'expected { foo: 123 } when delayed a little bit to equal { foo: 787 }\n' +
'\n' +
'{\n' +
' foo: 123 // should equal 787\n' +
'}'
));
});

it('should support diffs in the error report', () => {
expect(
function() {
Expand Down