开玩笑的魔术数量比较失败了... tobe

发布于 2025-02-12 15:37:27 字数 506 浏览 0 评论 0原文

我正在测试以确保buffer的魔术数字是zip格式。

我是通过提取缓冲区的第一个4个字节来串起的,并与pk的魔术数进行比较。

   const zipMagicNumber: string = 'PK'
   const uploadedMagicNumber: string = uploadMock.mock.calls[0][0].Body.subarray(0, 4).toString()

   expect(zipMagicNumber).toBe(uploadedMagicNumber)

但是测试失败了:

    expect(received).toBe(expected) // Object.is equality

    Expected: "PK"
    Received: "PK"

这些都是相同的值,并且都是字符串。我想念什么吗?

I'm testing to ensure a Buffer's magic number is zip format.

I'm doing this by extracting the buffer's first 4 bytes to string and comparing against the magic number for zip which is PK.

   const zipMagicNumber: string = 'PK'
   const uploadedMagicNumber: string = uploadMock.mock.calls[0][0].Body.subarray(0, 4).toString()

   expect(zipMagicNumber).toBe(uploadedMagicNumber)

But the test is failing with:

    expect(received).toBe(expected) // Object.is equality

    Expected: "PK"
    Received: "PK"

These are the both the same values and are both strings. Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等往事风中吹 2025-02-19 15:37:27

一些隐藏的字符引起了比较错误,因此,对于可读性,我移至使用十六进制比较,现在已经通过了:

const zipMagicNumber = Buffer.from('504B0304', 'hex').toString('hex')
const uploadedMagicNumber = uploadMock.mock.calls[0][0].Body.subarray(0, 4).toString('hex')

expect(zipMagicNumber).toBe(uploadedMagicNumber)

Some hidden characters were causing the comparison error, so for legibility I moved to using hex comparisons instead and now it's passing:

const zipMagicNumber = Buffer.from('504B0304', 'hex').toString('hex')
const uploadedMagicNumber = uploadMock.mock.calls[0][0].Body.subarray(0, 4).toString('hex')

expect(zipMagicNumber).toBe(uploadedMagicNumber)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文