From fe4316130d7697edbe49f59b0850ced57c9e1ce9 Mon Sep 17 00:00:00 2001 From: Miguel Jimenez Esun Date: Mon, 5 Feb 2018 16:07:48 +0000 Subject: [PATCH] Support deletion in process.env too --- CHANGELOG.md | 9 +++++++- .../__tests__/create_process_object.test.js | 18 +++++++++++++++ .../jest-util/src/create_process_object.js | 22 ++++++++++++++++++- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 907bf592e760..9531b5f7f67f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`) diff --git a/packages/jest-util/src/__tests__/create_process_object.test.js b/packages/jest-util/src/__tests__/create_process_object.test.js index 0c5c2036c616..dafb1ef4578e 100644 --- a/packages/jest-util/src/__tests__/create_process_object.test.js +++ b/packages/jest-util/src/__tests__/create_process_object.test.js @@ -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', () => { @@ -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); }); diff --git a/packages/jest-util/src/create_process_object.js b/packages/jest-util/src/create_process_object.js index d31c184e5556..a7442f1ec19d 100644 --- a/packages/jest-util/src/create_process_object.js +++ b/packages/jest-util/src/create_process_object.js @@ -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];