在Jest中将toMatchObject与stringMatching(regexp)一起使用

时间:2018-08-30 08:15:55

标签: typescript jestjs

我正在使用JEST匹配器toMatchObject来确保对象包含多个属性,并且某些值是静态的,而其他一些值应与特定的regexp匹配

问题是:当静态值不匹配时,即使它们很好,输出也会显示regexp值不匹配

代码:

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", 
  })
})

输出:

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",
  }

我想在这里只看到不匹配的值,因为regexp很好:

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

example

1 个答案:

答案 0 :(得分:1)

这似乎是个开玩笑的错误,https://github.com/facebook/jest/issues/6928