Skip to content

Commit

Permalink
Fix TextEncoder.encode not referencing same global Uint8Array constru…
Browse files Browse the repository at this point in the history
…ctor (#9261)
  • Loading branch information
wsmd authored and SimenB committed Dec 7, 2019
1 parent f4940ae commit eb19283
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

Expand Down
21 changes: 21 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2067,6 +2067,20 @@ exports[`.toEqual() {pass: false} expect([1]).toEqual([2]) 1`] = `
<d> ]</>
`;

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

<g>- Expected - 1</>
<r>+ Received + 1</>

<d> Uint8Array [</>
<d> 97,</>
<d> 98,</>
<g>- 100,</>
<r>+ 99,</>
<d> ]</>
`;

exports[`.toEqual() {pass: false} expect({"0": "a", "1": "b", "2": "c"}).toEqual("abc") 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>

Expand Down Expand Up @@ -2558,6 +2572,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
2 changes: 2 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])],
Expand Down Expand Up @@ -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')],
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);
});
}
});
5 changes: 5 additions & 0 deletions 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 Down

0 comments on commit eb19283

Please sign in to comment.