Skip to content

Commit

Permalink
Fix buffer property is not ArrayBuffer issue. (jestjs#7626)
Browse files Browse the repository at this point in the history
  • Loading branch information
H1Gdev authored and captain-yossarian committed Jul 18, 2019
1 parent caf6670 commit d9cd872
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
- `[babel-jest]` Set `cwd` to be resilient to it changing during the runtime of the tests ([#7574](https://github.com/facebook/jest/pull/7574))
- `[jest-snapshot]` Write and read snapshots from disk even if `fs` is mocked ([#7080](https://github.com/facebook/jest/pull/7080))
- `[jest-config]` Normalize `config.cwd` and `config.rootDir` using `realpath ([#7598](https://github.com/facebook/jest/pull/7598))
- `[jest-environment-node]` Fix buffer property is not ArrayBuffer issue. ([#7626](https://github.com/facebook/jest/pull/7626))

### Chore & Maintenance

Expand Down
20 changes: 16 additions & 4 deletions e2e/__tests__/env.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,37 @@ const getLog = result => result.stdout.split('\n')[1].trim();

describe('Environment override', () => {
it('uses jsdom when specified', () => {
const result = runJest('env-test', ['--env=jsdom']);
const result = runJest('env-test', ['--env=jsdom', 'env.test.js']);
expect(result.status).toBe(0);
expect(getLog(result)).toBe('WINDOW');
});

it('uses node as default from package.json', () => {
const result = runJest('env-test');
const result = runJest('env-test', ['env.test.js']);
expect(result.status).toBe(0);
expect(getLog(result)).toBe('NO WINDOW');
});

it('uses node when specified', () => {
const result = runJest('env-test', ['--env=node']);
const result = runJest('env-test', ['--env=node', 'env.test.js']);
expect(result.status).toBe(0);
expect(getLog(result)).toBe('NO WINDOW');
});

it('fails when the env is not available', () => {
const result = runJest('env-test', ['--env=banana']);
const result = runJest('env-test', ['--env=banana', 'env.test.js']);
expect(result.status).toBe(1);
});
});

describe('Environment equivalent', () => {
it('uses jsdom', () => {
const result = runJest('env-test', ['--env=jsdom', 'equivalent.test.js']);
expect(result.status).toBe(0);
});

it('uses node', () => {
const result = runJest('env-test', ['--env=node', 'equivalent.test.js']);
expect(result.status).toBe(0);
});
});
14 changes: 14 additions & 0 deletions e2e/env-test/__tests__/equivalent.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';

test('Buffer', () => {
const bufFromArray = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
expect(bufFromArray.buffer instanceof ArrayBuffer).toBeTruthy();
const bufFromArrayBuffer = Buffer.from(new ArrayBuffer(6));
expect(bufFromArrayBuffer.buffer instanceof ArrayBuffer).toBeTruthy();
});
1 change: 1 addition & 0 deletions packages/jest-environment-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class NodeEnvironment {
global.clearTimeout = clearTimeout;
global.setInterval = setInterval;
global.setTimeout = setTimeout;
global.ArrayBuffer = ArrayBuffer;
// URL and URLSearchParams are global in Node >= 10
if (typeof URL !== 'undefined' && typeof URLSearchParams !== 'undefined') {
/* global URL, URLSearchParams */
Expand Down

0 comments on commit d9cd872

Please sign in to comment.