jest-diff returns null if one string is multiline but the other is not

Created on 25 Feb 2018  路  7Comments  路  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?

bug

What is the current behavior?

https://repl.it/@sompylasar/jest-diff-multiline-vs-oneline
[email protected]

----------- a === "foo\nbar\nbaz"
foo
bar
baz
----------- b === "foo\n"
foo

----------- diff:
- Expected
+ Received

  foo
- bar
- baz
+ 
-----------

----------- a === "foo\nbar\nbaz"
foo
bar
baz
----------- b === "foo"
foo
----------- diff:
null
-----------

----------- a === "foo"
foo
----------- b === "foo\n"
foo

----------- diff:
null
-----------

----------- a === "foo"
foo
----------- b === "foo\nbar"
foo
bar
----------- diff:
null
-----------

What is the expected behavior?

----------- a === "foo\nbar\nbaz"
foo
bar
baz
----------- b === "foo\n"
foo

----------- diff:
- Expected
+ Received

  foo
- bar
- baz
+ 
-----------

----------- a === "foo\nbar\nbaz"
foo
bar
baz
----------- b === "foo"
foo
----------- diff:
- Expected
+ Received

  foo
- bar
- baz
-----------

----------- a === "foo"
foo
----------- b === "foo\n"
foo

----------- diff:
- Expected
+ Received

  foo
+ 
-----------

----------- a === "foo"
foo
----------- b === "foo\nbar"
foo
bar
----------- diff:
- Expected
+ Received

  foo
+ bar
-----------

Most helpful comment

I think we should fix this before releasing Jest 23.

whether it will seem like a step backward in developer experience if it reports a failure as follows

We can always improve the error message in Jest afterwards, but making jest-diff behave correctly doesn't have to be blocked by it, IMO.

All 7 comments

@pedrottimark

I think this bug is actually worse than described. The function also returns null if both strings are single-line. The only case when the method actually works _correctly_ is when both strings are multi-line.

Shouldn't it be valid to compare two single line strings?

Actually, it looks like this behavior is intentional:
https://github.com/facebook/jest/blob/3c65b72d88744a24d1eb985f147707b5d1c532dd/packages/jest-diff/src/__tests__/diff.test.js#L84-L91

But it doesn't seem expected or intuitive to me.

The tests were added later by @pedrottimark to document the behavior. Here's a rough explanation: https://github.com/jest-community/snapshot-diff/pull/27#discussion_r165079275

All in all, looks like it's about time to fix this and make it right for a generic use case, not only for expect matchers.

Yeah, we all agree to remove the special case. Something that has been in my slow cooker while making prerequisite changes in expect package (but sadly with no heat for several weeks) has been whether it will seem like a step backward in developer experience if it reports a failure as follows:

expect(expected).toBe(received)

- Expected
+ Received

- single_line_expected
+ single-line-received

I think we should fix this before releasing Jest 23.

whether it will seem like a step backward in developer experience if it reports a failure as follows

We can always improve the error message in Jest afterwards, but making jest-diff behave correctly doesn't have to be blocked by it, IMO.

Was this page helpful?
0 / 5 - 0 ratings