Chai: expect(Buffer).to.be.a('Buffer') incorrectly fails

Created on 19 Aug 2017  Â·  5Comments  Â·  Source: chaijs/chai

As on tin, but best explained by example:

Test case:

    it('should be a buffer', () => {
        let buf = Buffer.alloc(0)

        expect(buf).to.be.a('buffer')
    })

result:

     AssertionError: expected <Buffer > to be a buffer

env is a linux system, with the following:

$ node -v
v8.4.0
$ npm ls | grep chai
├─┬ [email protected]

Most helpful comment

@damianb btw you can workaround this by using the instanceof assertion

const buf = Buffer.alloc(0)
expect(buf).to.be.instanceof(Buffer)

All 5 comments

Thanks for the issue @damianb. Chai's type-detect module is currently classifying Node.js buffer objects as the underlying Uint8Array due to these lines.

My initial feeling is that type-detect should distinguish Node.js buffer objects from Unit8Arrays, especially given the "subtle incompatibilities" between the two documented here. But I don't use either of these very often, so would love to hear some more opinions.

I agree that buffers should be distinguishable: Uint8Array may seem like an implementation detail. I guess Node should set @@toStringTag to "Buffer".

@damianb btw you can workaround this by using the instanceof assertion

const buf = Buffer.alloc(0)
expect(buf).to.be.instanceof(Buffer)

@vieiralucas Thanks - ended up picking out that workaround myself after a little head scratching.

Figured it was worth still having the issue open, however - not sure if it merits further looking at, but that's your team's call.

Hey @damianb thanks for the issue.

We've added this to our Roadmap https://github.com/chaijs/chai/projects/2! We'll be releasing chai 5 soon, but for now I'll close this issue because it is tracked on our roadmap.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xareelee picture xareelee  Â·  3Comments

jockster picture jockster  Â·  4Comments

basherr picture basherr  Â·  4Comments

zzzgit picture zzzgit  Â·  3Comments

corybill picture corybill  Â·  4Comments