Skip to content

Commit

Permalink
Update reference of global.Uint8Array
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmd committed Dec 6, 2019
1 parent c4e342f commit bfffce0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2558,6 +2558,13 @@ Expected: not <g>[1]</>

`;

exports[`.toEqual() {pass: true} expect([97, 98, 99]).not.toEqual([97, 98, 99]) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toEqual<d>(</><g>expected</><d>) // deep equality</>

Expected: not <g>[97, 98, 99]</>

`;

exports[`.toEqual() {pass: true} expect([Function anonymous]).not.toEqual(Any<Function>) 1`] = `
<d>expect(</><r>received</><d>).</>not<d>.</>toEqual<d>(</><g>expected</><d>) // deep equality</>

Expand Down
16 changes: 1 addition & 15 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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')],
Expand Down Expand Up @@ -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)],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
});
}
});
6 changes: 5 additions & 1 deletion packages/jest-environment-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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') {
Expand Down

0 comments on commit bfffce0

Please sign in to comment.