From bfffce02922d67d68c77d4a00b1b46362a5f2b40 Mon Sep 17 00:00:00 2001 From: Waseem Dahman Date: Thu, 5 Dec 2019 19:17:00 -0500 Subject: [PATCH] Update reference of global.Uint8Array --- .../__snapshots__/matchers.test.js.snap | 7 +++++++ packages/expect/src/__tests__/matchers.test.js | 16 +--------------- .../src/__tests__/node_environment.test.ts | 8 ++++++++ packages/jest-environment-node/src/index.ts | 6 +++++- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 162652bbe6b4..d9f4639c5e8f 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -2558,6 +2558,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 7532b4edeb4c..99dd7e2555a8 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -24,7 +24,6 @@ afterAll(() => { /* global BigInt */ const isBigIntDefined = typeof BigInt === 'function'; -const isTextEncoderDefined = typeof TextEncoder === 'function'; it('should throw if passed two arguments', () => { expect(() => jestExpect('foo', 'bar')).toThrow( @@ -690,6 +689,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')], @@ -735,20 +735,6 @@ describe('.toEqual()', () => { }); }); - if (isTextEncoderDefined) { - [ - [new Uint8Array([97, 98, 99]), new Uint8Array([97, 98, 99])], - [new TextEncoder().encode('abc'), new Uint8Array([97, 98, 99])], - ].forEach(([a, b]) => { - test(`{pass: true} expect(${stringify(a)}).not.toEqual(${stringify( - b, - )})`, () => { - jestExpect(a).toEqual(b); - expect(() => jestExpect(a).not.toEqual(b)).toThrowError('toEqual'); - }); - }); - } - if (isBigIntDefined) { [ [BigInt(1), BigInt(1)], 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 dc5fcb55e7e0..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; @@ -58,7 +63,6 @@ class NodeEnvironment implements JestEnvironment { ) { global.TextEncoder = TextEncoder; global.TextDecoder = TextDecoder; - global.Uint8Array = Uint8Array; } // queueMicrotask is global in Node >= 11 if (typeof queueMicrotask !== 'undefined') {