feross / feross/buffer

Functions that use Buffer will have TypeError in jest unit testing

Open
#289 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.9k
Forks
260
PR merge metrics
No merged PRs in 30d

Description

My function contains the following code :

```javascript
// data here is a Buffer obj example: data = Buffer('xxxx', 'base64')
var isBuffer = typeof data.readUInt32BE === 'function' && typeof data.slice === 'function';
if (isBuffer) {
this.highStart = data.readUInt32BE(0);
this.errorValue = data.readUInt32BE(4);
uncompressedLength = data.readUInt32BE(8);
data = data.slice(12);
} else {
var view = new DataView(data.buffer);
this.highStart = view.getUint32(0);
this.errorValue = view.getUint32(4);
uncompressedLength = view.getUint32(8);
data = data.subarray(12);
} // double inflate the actual trie data
```
I use `"buffer": "^6.0.3"`

I wrote a jest unit test for my function. It can pass when I run in the node environment, but if I set testEnvironment to jsdom, an error will appear as shown in the figure below.

![image](https://user-images.githubusercontent.com/27608002/110729621-022b6d00-825a-11eb-8a59-7f1519fa815e.png)

Debug found that it seems that fromObject in buffer-es6 cannot handle this ArrayBuffer object. Is there any good way to circumvent this problem?

![image](https://user-images.githubusercontent.com/27608002/110731467-53892b80-825d-11eb-8b5f-93a506e6b3ae.png)

I don’t know if this is a jsdom problem or a Buffer problem?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.