Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest breaks Buffer instanceof Uint8Array #4422

Closed
dignifiedquire opened this issue Sep 5, 2017 · 13 comments
Closed

Jest breaks Buffer instanceof Uint8Array #4422

dignifiedquire opened this issue Sep 5, 2017 · 13 comments

Comments

@dignifiedquire
Copy link
Contributor

Do you want to request a feature or report a bug?
Bug

What is the current behavior?

In nodejs (8.4.0)

new Buffer('hello') instanceof Uint8Array
// => true

In jest (21.0.1)

new Buffer('hello') instanceof Uint8Array
// => false
new Buffer('hello').buffer instanceof Uint8Array
// => false

This breaks quite a few modules that assume (as they should be able to) that node buffers are Uint8Arrays.

@dignifiedquire
Copy link
Contributor Author

Update, tried setting testEnvironment: node explicitly, still the same issue

dignifiedquire added a commit to dignifiedquire/jest that referenced this issue Sep 5, 2017
passing a set of commong globals through to the env to ensure instanceof checks work as expected.

Fixes jestjs#4422
@dignifiedquire
Copy link
Contributor Author

The issue is that jest-util doesn't pass all necessary globals through.

@cpojer
Copy link
Member

cpojer commented Sep 5, 2017

This is unfortunately a side-effect of Jest pulling in the node libraries from the parent context, but having its own instances of UInt8Array in the child contexts. One thing you could try is to create your own environment that runs all tests in the main process, or overwrites UInt8Array in the child context with the one in the parent context. We don't have a good fix for this yet.

@dignifiedquire
Copy link
Contributor Author

Passing more globals through to the child process works in my tests, see #4423. Currently I am not seeing a big drawback to doing that, except that that list needs to be maintained. But I might be missing some context there.

@dignifiedquire
Copy link
Contributor Author

We cannot pull these globals from the parent context into the vm contexts unfortunately

Why the requirement of having the parent Buffer but the child globals for things like Uint8Array?

@cpojer
Copy link
Member

cpojer commented Sep 5, 2017

This is not expected, it is a bug, but because Jest tries to keep individual tests sandboxes and isolated, we cannot leak more things into the vm. Ideally we wrap all core libraries so that we swap out the prototypes later, like myBuffer.__protot__ = Uint8Array or re-evaluate all of node's core library code inside the vm context, but pulling them in from the parent is unfortunately not the right general solution. Using your own test environment should provide an escape hatch for you that should work reasonably well, as long as you know about the trade offs.

@dignifiedquire
Copy link
Contributor Author

Using your own test environment should provide an escape hatch for you that should work reasonably well, as long as you know about the trade offs.

Makes sense, thank you 👍

@dignifiedquire
Copy link
Contributor Author

Custom environment works like a charm, for anyone coming here I created this: https://github.com/ipfs/jest-environment-aegir

@cpojer
Copy link
Member

cpojer commented Sep 5, 2017

Thanks for working on this env and sharing it with everybody :) I'll close this issue as I know it's tracked somewhere on our issue tracker but I can't find it right now :( I'm sure somebody in the future will reference the two together.

@cpojer cpojer closed this as completed Sep 5, 2017
timrogers pushed a commit to timrogers/tweetnacl-js that referenced this issue Jan 11, 2019
This type check is incompatible with Jest because it injects
its own version of Uint8Array into the environment which is
different.

See jestjs/jest#4422.

The tests do not pass with this change.
TimWolla added a commit to TimWolla/forge that referenced this issue Jul 23, 2019
While this technically is not necessary, because node.js' Buffer
inherits from Uint8Array this contract is broken by jest.
See jestjs/jest#4422.

Add a `message instanceof NativeBuffer` to the function to fix the issue,
it does no harm and *technically* would be the more correct check, because
for node.js passing a raw Uint8Array does not return a Buffer.
davidlehn pushed a commit to digitalbazaar/forge that referenced this issue Sep 5, 2019
While this technically is not necessary, because node.js' Buffer
inherits from Uint8Array this contract is broken by jest.
See jestjs/jest#4422.

Add a `message instanceof NativeBuffer` to the function to fix the issue,
it does no harm and *technically* would be the more correct check, because
for node.js passing a raw Uint8Array does not return a Buffer.
yoursunny added a commit to yoursunny/NDNts that referenced this issue Sep 24, 2019
rkalis added a commit to CashScript/cashscript that referenced this issue Jan 14, 2020
- Remove mocha/chai/nyc dependencies
- Add jest + ts-jest dependencies
- Update eslintrc for jest
- Move mocha/nyc configs to jest config
- Add custom environment to fix Uint8Array issues
  (jestjs/jest#4422)
- Add custom reporters for cleaner output
- Update test files
- Update npm scripts
- Break up e2e tests into separate files for concurrency
@valamidev
Copy link

As a possible solution:

expect(Object.prototype.toString.call(new Uint8Array()).toEqual('[object Uint8Array]'); // PASS

mnzaki added a commit to jolocom/jolocom-sdk that referenced this issue Oct 8, 2020
so no recovery tests and no using jolo identities
this is because of jestjs/jest#4422 which
breaks hdkey
mnzaki added a commit to jolocom/jolo-did-method that referenced this issue Jan 12, 2021
@kevincox
Copy link

This should probably be reopened or a link to the other tracking issue should be found.

@kevincox
Copy link

kevincox commented Jan 30, 2021

The fix is to create a jest.config.js with the following. (Example)

export default {
	globals: {
		Uint8Array: Uint8Array,
	},
};

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2021
nima-ap referenced this issue in cthackers/adm-zip Jun 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants