-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathcreate_process_object.test.js
More file actions
76 lines (57 loc) · 2.58 KB
/
create_process_object.test.js
File metadata and controls
76 lines (57 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* 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);
});
it('checks that process.env works as expected on Linux platforms', () => {
Object.defineProperty(process, 'platform', {get: () => 'linux'});
// Existing properties inside process.env are copied to the fake environment.
process.env.PROP_STRING = 'foo';
process.env.PROP_NUMBER = 3;
process.env.PROP_UNDEFINED = undefined;
const fake = createProcessObject().env;
// All values converted to strings.
expect(fake.PROP_STRING).toBe('foo');
expect(fake.PROP_NUMBER).toBe('3');
expect(fake.PROP_UNDEFINED).toBe('undefined');
// Mac and Linux are case sensitive.
expect(fake.PROP_string).toBe(undefined);
// Added properties to the fake object are not added to the real one.
fake.PROP_ADDED = 'new!';
expect(fake.PROP_ADDED).toBe('new!');
expect(process.env.PROP_ADDED).toBe(undefined);
});
it('checks that process.env works as expected in Windows platforms', () => {
Object.defineProperty(process, 'platform', {get: () => 'win32'});
// Windows is not case sensitive when it comes to property names.
process.env.PROP_STRING = 'foo';
const fake = createProcessObject().env;
expect(fake.PROP_STRING).toBe('foo');
expect(fake.PROP_string).toBe('foo');
// Inherited methods, however, are not affected by case insensitiveness.
expect(typeof fake.toString).toBe('function');
expect(typeof fake.valueOf).toBe('function');
expect(typeof fake.tostring).toBe('undefined');
expect(typeof fake.valueof).toBe('undefined');
});