javascript - 如何在 Jest 自定义匹配器中打印漂亮的对象差异?

标签 javascript jestjs

我正在编写一个自定义的 Jest 匹配器来以某种任意方式比较对象:

expect.extend({
  toTorgle(received, expected) {
    ...
    return {
      pass: false,
      message: () => "expect(received).toTorgle(expected):" + ???
    }
  }
})
如何在两个对象之间打印一个很好的对象差异,就像我得到的那个(内置匹配器)expect(received).toEqual(expected) ?

最佳答案

根据Jest-Platform document , 你可以使用 jest-diff得到一个“ pretty-print ”的字符串,说明两个参数之间的差异。
您的 message函数将变为:

const { diff } = require('jest-diff'); // already available if Jest is installed

// ...

message: () => "expect(received).toTorgle(expected):" + diff(received, expected),

关于javascript - 如何在 Jest 自定义匹配器中打印漂亮的对象差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70584871/

相关文章:

c# - 在 javascript 函数中添加脚本 src

javascript - (开源)JavaScript 原型(prototype) OO 示例

unit-testing - 如何使用 WebStorm 调试 Vue CLI 3 单元测试?调试器没有命中断点

javascript - Angular 2 ng 用于相反顺序和相同索引

javascript - 使用jquery根据大小获取元素位置

javascript - 在 package.json 中更改代理不起作用

node.js - 是否可以使用 Jest 模拟本地函数

javascript - 如何模拟和测试 Axios 拒绝的 promise ?

javascript - 如何获取 H1 标签的值并使用 Jest 和 RTL 对其进行测试

node.js - 用 Jest 模拟 express.Router()