我正在使用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",