-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite how common globals are installed in "global" (#4904)
* Rewrite how common globals are installed in "global" * Refactor code: separate deep copy, process creation, and add unit test * Fix the fact that Jasmine requires access to the real process object
- Loading branch information
Showing
8 changed files
with
213 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/jest-util/src/__tests__/create_process_object.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. 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. | ||
*/ | ||
|
||
import EventEmitter from 'events'; | ||
import createProcessObject from '../create_process_object'; | ||
|
||
it('creates a process object that looks like the original one', () => { | ||
const fakeProcess = createProcessObject(); | ||
|
||
// "process" inherits from EventEmitter through the prototype chain. | ||
expect(fakeProcess instanceof EventEmitter).toBe(true); | ||
|
||
// They look the same, but they are NOT the same (deep copied object). The | ||
// "_events" property is checked to ensure event emitter properties are | ||
// properly copied. | ||
['argv', 'env', '_events'].forEach(key => { | ||
expect(fakeProcess[key]).toEqual(process[key]); | ||
expect(fakeProcess[key]).not.toBe(process[key]); | ||
}); | ||
|
||
// Check that process.stdout/stderr are the same. | ||
expect(process.stdout).toBe(fakeProcess.stdout); | ||
expect(process.stderr).toBe(fakeProcess.stderr); | ||
}); | ||
|
||
it('fakes require("process") so it is equal to "global.process"', () => { | ||
expect(require('process') === process).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. 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. | ||
* | ||
*/ | ||
|
||
import deepCyclicCopy from '../deep_cyclic_copy'; | ||
|
||
it('returns the same value for primitive or function values', () => { | ||
const fn = () => {}; | ||
|
||
expect(deepCyclicCopy(undefined)).toBe(undefined); | ||
expect(deepCyclicCopy(null)).toBe(null); | ||
expect(deepCyclicCopy(true)).toBe(true); | ||
expect(deepCyclicCopy(42)).toBe(42); | ||
expect(Number.isNaN(deepCyclicCopy(NaN))).toBe(true); | ||
expect(deepCyclicCopy('foo')).toBe('foo'); | ||
expect(deepCyclicCopy(fn)).toBe(fn); | ||
}); | ||
|
||
it('does not execute getters/setters, but copies them', () => { | ||
const fn = jest.fn(); | ||
const obj = { | ||
get foo() { | ||
fn(); | ||
}, | ||
}; | ||
const copy = deepCyclicCopy(obj); | ||
|
||
expect(Object.getOwnPropertyDescriptor(copy, 'foo')).toBeDefined(); | ||
expect(fn).not.toBeCalled(); | ||
}); | ||
|
||
it('copies symbols', () => { | ||
const symbol = Symbol('foo'); | ||
const obj = {[symbol]: 42}; | ||
|
||
expect(deepCyclicCopy(obj)[symbol]).toBe(42); | ||
}); | ||
|
||
it('copies arrays as array objects', () => { | ||
const array = [null, 42, 'foo', 'bar', [], {}]; | ||
|
||
expect(deepCyclicCopy(array)).toEqual(array); | ||
expect(Array.isArray(deepCyclicCopy(array))).toBe(true); | ||
}); | ||
|
||
it('handles cyclic dependencies', () => { | ||
const cyclic = {a: 42, subcycle: {}}; | ||
|
||
cyclic.subcycle.baz = cyclic; | ||
cyclic.bar = cyclic; | ||
|
||
expect(() => deepCyclicCopy(cyclic)).not.toThrow(); | ||
|
||
const copy = deepCyclicCopy(cyclic); | ||
|
||
expect(copy.a).toBe(42); | ||
expect(copy.bar).toEqual(copy); | ||
expect(copy.subcycle.baz).toEqual(copy); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. 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. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import deepCyclicCopy from './deep_cyclic_copy'; | ||
|
||
export default function() { | ||
const process = require('process'); | ||
const newProcess = deepCyclicCopy(process); | ||
|
||
// $FlowFixMe: Add the symbol for toString objects. | ||
newProcess[Symbol.toStringTag] = 'process'; | ||
|
||
// Sequentially execute all constructors over the object. | ||
let proto = process; | ||
|
||
while ((proto = Object.getPrototypeOf(proto))) { | ||
if (typeof proto.constructor === 'function') { | ||
proto.constructor.call(newProcess); | ||
} | ||
} | ||
|
||
return newProcess; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) 2017-present, Facebook, Inc. 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. | ||
* | ||
* @flow | ||
*/ | ||
|
||
export default function deepCyclicCopy( | ||
object: any, | ||
cycles: WeakMap<any, any> = new WeakMap(), | ||
) { | ||
if (typeof object !== 'object' || object === null) { | ||
return object; | ||
} | ||
|
||
let newObject; | ||
|
||
if (Array.isArray(object)) { | ||
newObject = []; | ||
} else { | ||
newObject = Object.create(Object.getPrototypeOf(object)); | ||
} | ||
|
||
cycles.set(object, newObject); | ||
|
||
// Copying helper function. Checks into the weak map passed to manage cycles. | ||
const copy = (key: string | Symbol) => { | ||
const descriptor = Object.getOwnPropertyDescriptor(object, key); | ||
const value = descriptor.value; | ||
|
||
if (descriptor.hasOwnProperty('value')) { | ||
if (cycles.has(value)) { | ||
descriptor.value = cycles.get(value); | ||
} else { | ||
descriptor.value = deepCyclicCopy(value, cycles); | ||
} | ||
|
||
// Allow tests to override whatever they need. | ||
descriptor.writable = true; | ||
} | ||
|
||
// Allow tests to override whatever they need. | ||
descriptor.configurable = true; | ||
|
||
try { | ||
Object.defineProperty(newObject, key, descriptor); | ||
} catch (err) { | ||
// Do nothing; this usually fails because a non-configurable property is | ||
// tried to be overridden with a configurable one (e.g. "length"). | ||
} | ||
}; | ||
|
||
// Copy string and symbol keys! | ||
Object.getOwnPropertyNames(object).forEach(copy); | ||
Object.getOwnPropertySymbols(object).forEach(copy); | ||
|
||
return newObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters