Use toMatchObject with stringMatching(regexp) in Jest

Viewed 3134

I'm using JEST matcher toMatchObject to ensure that object contains several properties and some values are static while some other values should match to specific regexp

The problem is: when static value doesn't match, output shows mismatches in regexp values as well, despite they are fine

Code:

test("asdf", async () => {
  const actual = {
    a: "a_value",
    b: "b_value", 
    c: "c_value"
  }
  expect(actual).toMatchObject({
    a: expect.stringMatching("[a-z]_value"), 
    b: "b_value", 
  })

  expect(actual).toMatchObject({
    a: expect.stringMatching("[a-z]_value"), 
    b: "B_VALUE", 
  })
})

Output:

Expected value to match object:
  {"a": StringMatching /[a-z]_value/, "b": "B_VALUE"}
Received:
  {"a": "a_value", "b": "b_value", "c": "c_value"}
Difference:
- Expected
+ Received

  Object {
-   "a": StringMatching /[a-z]_value/,
-   "b": "B_VALUE",
+   "a": "a_value",
+   "b": "b_value",
  }

I want to see in output here only mismatched values, since regexp is fine:

   Object {
    -   "b": "B_VALUE",
    +   "b": "b_value",

example

1 Answers
Related