Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TextEncoder.encode not referencing same global Uint8Array constructor #9261

Merged
merged 7 commits into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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