是否有一种方法来扩展上下文行来比较Jest中的字符串?



我在Jest中进行了一个NodeJS测试,该测试将非常长的字符串与许多行进行比较。在我的实际示例中,这是由打印精美的JSON产生的,但下面的简单示例说明了问题:

describe('Stuff', () => {
it('should make it clear where the diff is but does not', () => {
const str1 = 'Helloxnxnxnxnxnxnxnxnxnxnxnxnworldxnxnxnxnxnxnxnxnxnxn'
const str2 = 'Helloxnxnxnxnxnxn123nxnxnxnxnxnxnworldxnxnxnxnxnxnxnxnxnxn'
expect(str1).toEqual(str2)
})
})

当我运行它时,我得到这个:

● Stuff › should make it clear where the diff is but does not
expect(received).toEqual(expected) // deep equality
- Expected  - 1
+ Received  + 0
@@ -2,11 +2,10 @@
x
x
x
x
x
- 123
x
x
x
x
x

在差异的两边有很多相似或相同的线,很难找出问题在哪里。我是否有办法控制或扩展上下文,或者它是硬编码为5前5后的规则?

不干净。在jest-diff中有选项可以设置:

https://github.com/facebook/jest/tree/main/packages/jest-diff选项然而,在撰写本文时,这些并没有从jest配置中传递-有一个开放的特性请求:

https://github.com/facebook/jest/issues/12576

因此,没有干净的方法来配置它。可以简单地编辑node_modules/jest-diff/build/normalizeDiffOptions.js中的值,但由于明显的原因,这是令人讨厌的。

最新更新