gpt4 book ai didi

typescript - 在 Jest 中使用 toMatchObject 和 stringMatching(regexp)

转载 作者:行者123 更新时间:2023-12-05 06:30:45 24 4
gpt4 key购买 nike

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

问题是:当静态值不匹配时,输出也会显示正则表达式值不匹配,尽管它们很好

代码:

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

我只想在此处的输出中看到不匹配的值,因为正则表达式没问题:

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

example

最佳答案

这似乎是 Jest 错误,https://github.com/facebook/jest/issues/6928 .

关于typescript - 在 Jest 中使用 toMatchObject 和 stringMatching(regexp),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52092233/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com