0

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?

alyx
  • 2,593
  • 6
  • 39
  • 64

1 Answers1

0

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)
alyx
  • 2,593
  • 6
  • 39
  • 64