jest: ArrayBuffer regression in node env

🐛 Bug Report

https://github.com/facebook/jest/pull/7626 introduced regression

expect(new Uint8Array([]).buffer).toBeInstanceOf(ArrayBuffer);

fails with

    Expected constructor: ArrayBuffer
    Received constructor: ArrayBuffer
    Received value: []

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 23
  • Comments: 28 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I lied. I came back. Workaround:

// __test-utils__/custom-jest-environment.js
// Stolen from: https://github.com/ipfs/jest-environment-aegir/blob/master/src/index.js
// Overcomes error from jest internals.. this thing: https://github.com/facebook/jest/issues/6248
"use strict";

const NodeEnvironment = require("jest-environment-node");

class MyEnvironment extends NodeEnvironment {
  constructor(config) {
    super(
      Object.assign({}, config, {
        globals: Object.assign({}, config.globals, {
          Uint32Array: Uint32Array,
          Uint8Array: Uint8Array,
          ArrayBuffer: ArrayBuffer,
        }),
      }),
    );
  }

  async setup() {}

  async teardown() {}

}

module.exports = MyEnvironment;

Then in package.json:

  "jest": {
    "testEnvironment": "__test-utils__/custom-jest-environment.js",
    ...
  },

@odbol You can also just add this

/**
 * @jest-environment node
 */

to the top of the file having this issue

i worked around this by adding jest-environment-jsdom@25 as a dev dep per https://github.com/facebook/jest/issues/7780#issuecomment-669828353

@odahcam @wi-ski

Thanks for sharing the workaround, but it seems to me that the solution is already out of date. I’ve just installed jest-environment-node@^26.0.1 and in jest.config.js I set:

module.exports = {
  // ...
  testEnvironment: 'node',
};

That’s it. I didn’t need to extend the NodeEnvironment and overload the constructor. It just worked.

Correct me if I’m wrong, but it seems that when jest-environment-node is installed then jest uses that module to configure the environment. So it looks like the bug is already fixed in jest-environment-node modlue.

Am I right?

Setting testEnvironment: 'node' in jest.config.js works for me too without using the workaround. I do not need to install jest-environment-node.

let t = Buffer.alloc(0)
t instanceof Uint8Array

true in jest-environment-jsdom@25 false in jest-environment-jsdom@26

This forced me to use mocha.

@SimenB

Execute the following code in each environment.(–env=node, --env=jsdom, and node).

const bufFromArray = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
bufFromArray instanceof Uint8Array;
const bufFromArrayBuffer = Buffer.from(new ArrayBuffer(6));
bufFromArrayBuffer instanceof Uint8Array;

In --env=node, results are not true.

It seems that Uint8Array also has the same issue as ArrayBuffer.

So, create-react-app doesn’t allow you to override testEnvironment. Does anyone know a way to apply the workaround when using create-react-app?

I tried using jest { setupGlobals } but that wouldn’t let me modify the globals config.

npm test

> my-app@0.1.0 test ***
> react-scripts test


Out of the box, Create React App only supports overriding these Jest options:

  • clearMocks
  • collectCoverageFrom
  • coveragePathIgnorePatterns
  • coverageReporters
  • coverageThreshold
  • displayName
  • extraGlobals
  • globalSetup
  • globalTeardown
  • moduleNameMapper
  • resetMocks
  • resetModules
  • restoreMocks
  • snapshotSerializers
  • testMatch
  • transform
  • transformIgnorePatterns
  • watchPathIgnorePatterns.

These options in your package.json Jest configuration are not currently supported by Create React App:

  • testEnvironment

If you wish to override other Jest options, you need to eject from the default setup. You can do so by running npm run eject but remember that this is a one-way operation. You may also file an issue with Create React App to discuss supporting more options out of the box.

I just had to put a ./ before the testEnvironment path:

  "jest": {
    "testEnvironment": "./__test-utils__/custom-jest-environment.js",
    ...
  },

I am still experiencing this issue with jest-environment-jsdom@27. I tried the solution of adding a custom environment, but then the behavior of VSCode’s jest extension becomes all wonky. It can’t figure out the status of the tests. Ughh