typescript - 在 Jest 中使用 toMatchObject 和 stringMatching(regexp)
问题描述
我正在使用 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",
解决方案
这似乎是开玩笑的错误, https://github.com/facebook/jest/issues/6928。
推荐阅读
- bash - 如何在 bash 中传递双引号“--参数值”字符串
- azure - 如何使用 api 将订阅的贡献者角色分配给服务主体或如何使用订阅对象
- php - nginx auth_request 示例
- c# - 将数据库连接字符串从 appconfig.json 迁移到环境变量
- javascript - 人脸检测未显示在正确位置
- javascript - Bokeh source.change.emit() Uncaught TypeError: value.toFixed
- python - Mypy 抱怨默认参数值与省略号不兼容
- postgresql - 插入或更新或删除后的postgresql触发器
- javascript - 如何在 Vue js 3 中使用 Vue Draggabale
- c# - PactNet 合同测试失败并出现 HttpRequestException 和 SocketException