Skip to content

Commit

Permalink
Support deletion in process.env too (#5466)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjesun committed Feb 5, 2018
1 parent d6781c3 commit 5229f35
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

### Features

* `[jest-util]` Add deletion to `process.env` as well
([#5466](https://github.com/facebook/jest/pull/5466))
* `[jest-util]` Add case-insensitive getters/setters to `process.env`
([#5465](https://github.com/facebook/jest/pull/5465))
* `[jest-mock]` Add util methods to create async functions.
([#5318](https://github.com/facebook/jest/pull/5318))

### Fixes

* `[jest-cli]` Hide interactive mode if there are no failed snapshot tests ([#5450](https://github.com/facebook/jest/pull/5450))
* `[jest-cli]` Add trailing slash when checking root folder
([#5464](https://github.com/facebook/jest/pull/5464))
* `[jest-cli]` Hide interactive mode if there are no failed snapshot tests
([#5450](https://github.com/facebook/jest/pull/5450))
* `[babel-jest]` Remove retainLines from babel-jest
([#5326](https://github.com/facebook/jest/pull/5439))
* `[jest-cli]` Glob patterns ignore non-`require`-able files (e.g. `README.md`)
Expand Down
18 changes: 18 additions & 0 deletions packages/jest-util/src/__tests__/create_process_object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ it('checks that process.env works as expected on Linux platforms', () => {

expect(fake.PROP_ADDED).toBe('new!');
expect(process.env.PROP_ADDED).toBe(undefined);

// You can delete properties, but they are case sensitive!
fake.prop = 'foo';
fake.PROP = 'bar';

expect(fake.prop).toBe('foo');
expect(fake.PROP).toBe('bar');

delete fake.PROP;

expect(fake.prop).toBe('foo');
expect(fake.PROP).toBe(undefined);
});

it('checks that process.env works as expected in Windows platforms', () => {
Expand All @@ -73,4 +85,10 @@ it('checks that process.env works as expected in Windows platforms', () => {

expect(typeof fake.tostring).toBe('undefined');
expect(typeof fake.valueof).toBe('undefined');

// You can delete through case-insensitiveness too.
delete fake.prop_string;

expect(fake.hasOwnProperty('PROP_STRING')).toBe(false);
expect(fake.hasOwnProperty('PROP_string')).toBe(false);
});
22 changes: 21 additions & 1 deletion packages/jest-util/src/create_process_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,28 @@ function createProcessEnv() {
const lookup = {};

const proxy = new Proxy(real, {
deleteProperty(target, key) {
for (const name in real) {
if (real.hasOwnProperty(name)) {
if (typeof key === 'string' && process.platform === 'win32') {
if (name.toLowerCase() === key.toLowerCase()) {
delete real[name];
delete lookup[name.toLowerCase()];
}
} else {
if (key === name) {
delete real[name];
delete lookup[name];
}
}
}
}

return true;
},

get(target, key) {
if ((typeof key === 'string') && (process.platform === 'win32')) {
if (typeof key === 'string' && process.platform === 'win32') {
return lookup[key in proto ? key : key.toLowerCase()];
} else {
return real[key];
Expand Down

0 comments on commit 5229f35

Please sign in to comment.