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

Opt-out special char escaping in result diff #5660

Closed
maciej-ka opened this issue Feb 25, 2018 · 3 comments · Fixed by #11654
Closed

Opt-out special char escaping in result diff #5660

maciej-ka opened this issue Feb 25, 2018 · 3 comments · Fixed by #11654

Comments

@maciej-ka
Copy link
Contributor

maciej-ka commented Feb 25, 2018

What

At the moment jest will always escape special char \ in diff output of the failed test and replace it with \\. I would love to have an option to disable this.

Having a test like this:

it('works', () => {
  const str = 'a\\nb';
  expect(str).toEqual('');
});

(note that console.log(str) will output a\nb)

Message is:

    expect(received).toEqual(expected)

    Expected value to equal:
      ""
    Received:
      "a\\nb"

And the desired output would be:

    expect(received).toEqual(expected)

    Expected value to equal:
      ""
    Received:
      "a\nb"

Why

In my test suite, diff will print a valid javascript.

It's about a library to write custom parsers. Example test is like this:

  test('basic match', () => {
    const syntax = many(char('a'));
    const actual = syntax(new Cursor('aa'));
    expect(pretty(actual)).toEqual(pretty(
      manyNode(0, 2,
        charNode(0, 1, 'a'),
        charNode(1, 2, 'a'))
    ));
  });

Function pretty takes a tree of js objects and returns a string that is a valid javascript.

When expected tree structure is complicated, it's very handy to start with empty expectation:

    expect(pretty(actual)).toEqual(pretty(
    ));

And copy-paste one from test failure message:

    Expected value to equal:
      undefined
    Received:
      "
    manyNode(0, 2,
      charNode(0, 1, 'a'),
      charNode(1, 2, 'a'))
    "

However, at the moment, there is no way jest can print \n in that message, because it will always escape it into \\n.

Posible solution

Make escaping of a special chars optional in pretty-format and introduce an option in jest that would be passed to that line of code.

@SimenB
Copy link
Member

SimenB commented Sep 17, 2018

Ok, landed the option to pretty-format. Now, we need to figure out some way to allow us to pass that option down to it (via jest-diff, I suppose). @rickhanlonii @thymikee @pedrottimark ideas?

@rickhanlonii
Copy link
Member

rickhanlonii commented Dec 2, 2018

IMO this should a custom matcher, with that you can pass the option directly to jest-diff

@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 Sep 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants