From eb192837d31ab1d3f02e322e288729bc5f04d844 Mon Sep 17 00:00:00 2001 From: Waseem Dahman Date: Sat, 7 Dec 2019 05:42:53 -0500 Subject: [PATCH] Fix TextEncoder.encode not referencing same global Uint8Array constructor (#9261) --- CHANGELOG.md | 3 ++- .../__snapshots__/matchers.test.js.snap | 21 +++++++++++++++++++ .../expect/src/__tests__/matchers.test.js | 2 ++ .../src/__tests__/node_environment.test.ts | 8 +++++++ packages/jest-environment-node/src/index.ts | 5 +++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 370578243102..2c2faa49d29b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,7 +52,9 @@ - `[jest-core]` Limit number of workers when creating haste maps in projects ([#9259](https://github.com/facebook/jest/pull/9259)) - `[jest-diff]` Do not inverse format if line consists of one change ([#8903](https://github.com/facebook/jest/pull/8903)) - `[jest-diff]` Rename some new options and change their default values ([#9077](https://github.com/facebook/jest/pull/9077)) +- `[jest-environment-node]` Fix `TextEncoder.encode` not referencing same global `Uint8Array` constructor ([#9261](https://github.com/facebook/jest/pull/9261)) - `[jest-fake-timers]` `getTimerCount` will not include cancelled immediates ([#8764](https://github.com/facebook/jest/pull/8764)) +- `[jest-jasmine2, jest-circus]` Improve error message format for Node's assert.fail ([#9262](https://github.com/facebook/jest/pull/9262)) - `[jest-leak-detector]` [**BREAKING**] Use `weak-napi` instead of `weak` package ([#8686](https://github.com/facebook/jest/pull/8686)) - `[jest-mock]` Fix for mockReturnValue overriding mockImplementationOnce ([#8398](https://github.com/facebook/jest/pull/8398)) - `[jest-reporters]` Make node-notifier an optional dependency ([#8918](https://github.com/facebook/jest/pull/8918)) @@ -69,7 +71,6 @@ - `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136)) - `[pretty-format]` Correctly detect memoized elements ([#9196](https://github.com/facebook/jest/pull/9196)) - `[jest-fake-timers]` Support `util.promisify` on `setTimeout` ([#9180](https://github.com/facebook/jest/pull/9180)) -- `[jest-jasmine2, jest-circus]` Improve error message format for Node's assert.fail ([#9262](https://github.com/facebook/jest/pull/9262)) ### Chore & Maintenance diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 162652bbe6b4..b811c13d7360 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -2067,6 +2067,20 @@ exports[`.toEqual() {pass: false} expect([1]).toEqual([2]) 1`] = ` ] `; +exports[`.toEqual() {pass: false} expect([97, 98, 99]).toEqual([97, 98, 100]) 1`] = ` +expect(received).toEqual(expected) // deep equality + +- Expected - 1 ++ Received + 1 + + Uint8Array [ + 97, + 98, +- 100, ++ 99, + ] +`; + exports[`.toEqual() {pass: false} expect({"0": "a", "1": "b", "2": "c"}).toEqual("abc") 1`] = ` expect(received).toEqual(expected) // deep equality @@ -2558,6 +2572,13 @@ Expected: not [1] `; +exports[`.toEqual() {pass: true} expect([97, 98, 99]).not.toEqual([97, 98, 99]) 1`] = ` +expect(received).not.toEqual(expected) // deep equality + +Expected: not [97, 98, 99] + +`; + exports[`.toEqual() {pass: true} expect([Function anonymous]).not.toEqual(Any) 1`] = ` expect(received).not.toEqual(expected) // deep equality diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index 97ad88b3333f..fb6223a9466f 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -488,6 +488,7 @@ describe('.toEqual()', () => { Immutable.Map({1: Immutable.Map({2: {a: 99}})}), Immutable.Map({1: Immutable.Map({2: {a: 11}})}), ], + [new Uint8Array([97, 98, 99]), new Uint8Array([97, 98, 100])], [{a: 1, b: 2}, jestExpect.objectContaining({a: 2})], [false, jestExpect.objectContaining({a: 2})], [[1, 3], jestExpect.arrayContaining([1, 2])], @@ -689,6 +690,7 @@ describe('.toEqual()', () => { Immutable.Map({1: Immutable.Map({2: {a: 99}})}), Immutable.Map({1: Immutable.Map({2: {a: 99}})}), ], + [new Uint8Array([97, 98, 99]), new Uint8Array([97, 98, 99])], [{a: 1, b: 2}, jestExpect.objectContaining({a: 1})], [[1, 2, 3], jestExpect.arrayContaining([2, 3])], ['abcd', jestExpect.stringContaining('bc')], diff --git a/packages/jest-environment-node/src/__tests__/node_environment.test.ts b/packages/jest-environment-node/src/__tests__/node_environment.test.ts index 35ee577c3a62..e43df37a1bf5 100644 --- a/packages/jest-environment-node/src/__tests__/node_environment.test.ts +++ b/packages/jest-environment-node/src/__tests__/node_environment.test.ts @@ -8,6 +8,8 @@ import NodeEnvironment = require('../'); import {makeProjectConfig} from '../../../../TestUtils'; +const isTextEncoderDefined = typeof TextEncoder === 'function'; + describe('NodeEnvironment', () => { it('uses a copy of the process object', () => { const env1 = new NodeEnvironment(makeProjectConfig()); @@ -49,4 +51,10 @@ describe('NodeEnvironment', () => { expect(env.fakeTimersLolex).toBeDefined(); }); + + if (isTextEncoderDefined) { + test('TextEncoder references the same global Uint8Array constructor', () => { + expect(new TextEncoder().encode('abc')).toBeInstanceOf(Uint8Array); + }); + } }); diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 83ad8a24067f..a3166920ceed 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -46,6 +46,11 @@ class NodeEnvironment implements JestEnvironment { global.setInterval = setInterval; global.setTimeout = setTimeout; global.ArrayBuffer = ArrayBuffer; + // TextEncoder (global or via 'util') references a Uint8Array constructor + // different than the global one used by users in tests. This makes sure the + // same constructor is referenced by both. + global.Uint8Array = Uint8Array; + // URL and URLSearchParams are global in Node >= 10 if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') { global.URL = URL;