dimanche 29 octobre 2017

String equality fails while testing with regex escaped expression

I am testing a function where it returns a regex escaped version of the string and hence in test I am testing it with if it equals to the escaped string, but it fails with saying

Expected value to match:
  "4**2"
Received:
  "4\\*\\*2"

This is the test I have written:

describe("escapeRegExp", () => {
  // required for quotes test
  test("Escapes all RegExp characters", () => {
    expect(escapeRegExp("4**2")).toMatch("4\*\*2");});

where escapeRegExp function returns "4\*\*2" but it expects it to be "4**2".

But when I use

expect(escapeRegExp("4**2")).toMatchSnapshot("4\*\*2"); it works fine.

Any idea why it fails while checking it with .toBe or toEqual() ??

Is there any other field/argument that I have to add to make it work with toEqual or toMatchSnapshot is the way to go for this ?

Aucun commentaire:

Enregistrer un commentaire