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

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

Closed
sompylasar opened this issue Feb 25, 2018 · 8 comments · Fixed by #6221
Closed

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

sompylasar opened this issue Feb 25, 2018 · 8 comments · Fixed by #6221
Assignees
Milestone

Comments

@sompylasar
Copy link

sompylasar commented Feb 25, 2018

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
jest-diff@22.1.0

----------- 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
-----------
@SimenB
Copy link
Member

SimenB commented Feb 25, 2018

@pedrottimark

@pedrottimark pedrottimark self-assigned this Feb 25, 2018
@bvaughn
Copy link
Contributor

bvaughn commented Mar 26, 2018

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?

@bvaughn
Copy link
Contributor

bvaughn commented Mar 26, 2018

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.

@thymikee
Copy link
Collaborator

thymikee commented Mar 26, 2018

The tests were added later by @pedrottimark to document the behavior. Here's a rough explanation: jest-community/snapshot-diff#27 (comment)

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.

@pedrottimark
Copy link
Contributor

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

@SimenB
Copy link
Member

SimenB commented May 1, 2018

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.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants