Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Opt-out special char escaping in result diff #5660

@maciej-ka

Description

@maciej-ka

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions