When using the JEST matcher toMatchObject
, my objective is to ensure that an object contains certain properties with static values, while other values should match specific regular expressions.
The issue I'm facing is that when a static value doesn't match, the output displays mismatches in the regular expression values as well, even though they are correct.
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 would like the output to only display mismatched values, ignoring the regular expression since it is correct:
Object {
- "b": "B_VALUE",
+ "b": "b_value",