From d17a9cc0d8affb9e22950c5c5c9673accc7492f8 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Tue, 5 Nov 2019 13:06:06 +0200 Subject: [PATCH 01/10] jest-utils: add a getter for process.domain Fixes #7247: Explicitly copy domain to new process --- .../jest-util/src/__tests__/createProcessObject.test.ts | 8 ++++++++ packages/jest-util/src/createProcessObject.ts | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/packages/jest-util/src/__tests__/createProcessObject.test.ts b/packages/jest-util/src/__tests__/createProcessObject.test.ts index c1658f793983..1af70c531084 100644 --- a/packages/jest-util/src/__tests__/createProcessObject.test.ts +++ b/packages/jest-util/src/__tests__/createProcessObject.test.ts @@ -6,6 +6,7 @@ */ import {EventEmitter} from 'events'; +import * as domain from 'domain'; let createProcessObject; @@ -105,3 +106,10 @@ it('checks that process.env works as expected in Windows platforms', () => { expect(fake.hasOwnProperty('PROP_STRING')).toBe(false); expect(fake.hasOwnProperty('PROP_string')).toBe(false); }); + +test('allows retrieving the current domain', () => { + const aDomain = domain.create(); + process.domain = aDomain; + + expect(createProcessObject().domain).toEqual(aDomain); +}); diff --git a/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index 53d18dd43cb5..e04f48a5a3b5 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -112,5 +112,11 @@ export default function() { newProcess.env = createProcessEnv(); newProcess.send = () => {}; + Object.defineProperty(newProcess, 'domain', { + get() { + return process.domain; + }, + }); + return newProcess; } From 598d75ad253a0fb39310c0a8d74f631ce2930e59 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Tue, 5 Nov 2019 13:31:27 +0200 Subject: [PATCH 02/10] update the change-log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5865b17d2e3c..d4ed6d402f58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ - `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898)) - `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049)) - `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890)) +- `[jest-utils]`Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136)) ### Chore & Maintenance From d4755a0f175e5319ba5fd5cdc388005c291c6b6e Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Tue, 5 Nov 2019 14:07:41 +0200 Subject: [PATCH 03/10] set domain using Object.defineProperty --- globalSetupFile.js | 7 +++++++ jest.config.js | 3 +-- .../jest-util/src/__tests__/createProcessObject.test.ts | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 globalSetupFile.js diff --git a/globalSetupFile.js b/globalSetupFile.js new file mode 100644 index 000000000000..7b3df08a2655 --- /dev/null +++ b/globalSetupFile.js @@ -0,0 +1,7 @@ +// Jest creates a copy of the global 'process' object and uses it instead of the one provided by node. +// Since 'require'-ing the 'domain' module has a side-effect that modifies "process" to make it work with domains, +// 'domain' has to be required before Jest performs the copy, i.e. in the global-setup phase represented by this file. + +module.exports = () => { + require('domain'); +}; diff --git a/jest.config.js b/jest.config.js index a39a9f4789f5..9b8cf6f429a5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -'use strict'; - module.exports = { collectCoverageFrom: [ '**/packages/*/**/*.js', @@ -20,6 +18,7 @@ module.exports = { '!**/vendor/**', '!e2e/**', ], + globalSetup: '/globalSetupFile.js', modulePathIgnorePatterns: [ 'examples/.*', 'packages/.*/build', diff --git a/packages/jest-util/src/__tests__/createProcessObject.test.ts b/packages/jest-util/src/__tests__/createProcessObject.test.ts index 1af70c531084..6604fb8b14b7 100644 --- a/packages/jest-util/src/__tests__/createProcessObject.test.ts +++ b/packages/jest-util/src/__tests__/createProcessObject.test.ts @@ -109,7 +109,7 @@ it('checks that process.env works as expected in Windows platforms', () => { test('allows retrieving the current domain', () => { const aDomain = domain.create(); - process.domain = aDomain; + Object.defineProperty(process, 'domain', {get: () => aDomain}); expect(createProcessObject().domain).toEqual(aDomain); }); From 9e2abf688b84f3faad2124766087076ad57d7c6f Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Tue, 5 Nov 2019 14:23:29 +0200 Subject: [PATCH 04/10] add facebook copyrights to globalSetupFile.js --- globalSetupFile.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/globalSetupFile.js b/globalSetupFile.js index 7b3df08a2655..5dd88ea88dc3 100644 --- a/globalSetupFile.js +++ b/globalSetupFile.js @@ -1,3 +1,10 @@ +/** + * 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. + */ + // Jest creates a copy of the global 'process' object and uses it instead of the one provided by node. // Since 'require'-ing the 'domain' module has a side-effect that modifies "process" to make it work with domains, // 'domain' has to be required before Jest performs the copy, i.e. in the global-setup phase represented by this file. From 4748227e3f87775f74bcf473ea5a65249b990f00 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Mon, 11 Nov 2019 11:30:29 +0200 Subject: [PATCH 05/10] convert unit test to e2e test --- e2e/__tests__/createProcessObject.test.ts | 14 ++++++++++++++ .../__tests__/createProcessObject.test.js | 14 ++++++++++++++ e2e/create-process-object/package.json | 6 ++++++ .../create-process-object/setup.js | 7 ------- jest.config.js | 6 +----- .../src/__tests__/createProcessObject.test.ts | 14 +++----------- packages/jest-util/src/createProcessObject.ts | 4 ++-- 7 files changed, 40 insertions(+), 25 deletions(-) create mode 100644 e2e/__tests__/createProcessObject.test.ts create mode 100644 e2e/create-process-object/__tests__/createProcessObject.test.js create mode 100644 e2e/create-process-object/package.json rename globalSetupFile.js => e2e/create-process-object/setup.js (64%) diff --git a/e2e/__tests__/createProcessObject.test.ts b/e2e/__tests__/createProcessObject.test.ts new file mode 100644 index 000000000000..c31747b63f45 --- /dev/null +++ b/e2e/__tests__/createProcessObject.test.ts @@ -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. + */ + +import runJest from '../runJest'; + +test('allows retrieving the current domain', () => { + const result = runJest('create-process-object'); + + expect(result.exitCode).toBe(0); +}); diff --git a/e2e/create-process-object/__tests__/createProcessObject.test.js b/e2e/create-process-object/__tests__/createProcessObject.test.js new file mode 100644 index 000000000000..81872d6fb05c --- /dev/null +++ b/e2e/create-process-object/__tests__/createProcessObject.test.js @@ -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. + */ + +const domain = require('domain'); + +test('allows retrieving the current domain', () => { + domain.create().run(() => { + expect(process.domain).not.toBeNull(); + }); +}); diff --git a/e2e/create-process-object/package.json b/e2e/create-process-object/package.json new file mode 100644 index 000000000000..be713777531e --- /dev/null +++ b/e2e/create-process-object/package.json @@ -0,0 +1,6 @@ +{ + "jest": { + "testEnvironment": "node", + "globalSetup": "/setup.js" + } +} diff --git a/globalSetupFile.js b/e2e/create-process-object/setup.js similarity index 64% rename from globalSetupFile.js rename to e2e/create-process-object/setup.js index 5dd88ea88dc3..7b3df08a2655 100644 --- a/globalSetupFile.js +++ b/e2e/create-process-object/setup.js @@ -1,10 +1,3 @@ -/** - * 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. - */ - // Jest creates a copy of the global 'process' object and uses it instead of the one provided by node. // Since 'require'-ing the 'domain' module has a side-effect that modifies "process" to make it work with domains, // 'domain' has to be required before Jest performs the copy, i.e. in the global-setup phase represented by this file. diff --git a/jest.config.js b/jest.config.js index 9b8cf6f429a5..9abbf1248713 100644 --- a/jest.config.js +++ b/jest.config.js @@ -18,7 +18,6 @@ module.exports = { '!**/vendor/**', '!e2e/**', ], - globalSetup: '/globalSetupFile.js', modulePathIgnorePatterns: [ 'examples/.*', 'packages/.*/build', @@ -64,8 +63,5 @@ module.exports = { transform: { '^.+\\.[jt]sx?$': '/packages/babel-jest', }, - watchPlugins: [ - 'jest-watch-typeahead/filename', - 'jest-watch-typeahead/testname', - ], + watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'], }; diff --git a/packages/jest-util/src/__tests__/createProcessObject.test.ts b/packages/jest-util/src/__tests__/createProcessObject.test.ts index 6604fb8b14b7..bea547f0d19f 100644 --- a/packages/jest-util/src/__tests__/createProcessObject.test.ts +++ b/packages/jest-util/src/__tests__/createProcessObject.test.ts @@ -5,8 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {EventEmitter} from 'events'; -import * as domain from 'domain'; +import { EventEmitter } from 'events'; let createProcessObject; @@ -43,7 +42,7 @@ it('fakes require("process") so it is equal to "global.process"', () => { }); it('checks that process.env works as expected on Linux platforms', () => { - Object.defineProperty(process, 'platform', {get: () => 'linux'}); + Object.defineProperty(process, 'platform', { get: () => 'linux' }); requireCreateProcessObject(); // Existing properties inside process.env are copied to the fake environment. @@ -82,7 +81,7 @@ it('checks that process.env works as expected on Linux platforms', () => { }); it('checks that process.env works as expected in Windows platforms', () => { - Object.defineProperty(process, 'platform', {get: () => 'win32'}); + Object.defineProperty(process, 'platform', { get: () => 'win32' }); requireCreateProcessObject(); // Windows is not case sensitive when it comes to property names. @@ -106,10 +105,3 @@ it('checks that process.env works as expected in Windows platforms', () => { expect(fake.hasOwnProperty('PROP_STRING')).toBe(false); expect(fake.hasOwnProperty('PROP_string')).toBe(false); }); - -test('allows retrieving the current domain', () => { - const aDomain = domain.create(); - Object.defineProperty(process, 'domain', {get: () => aDomain}); - - expect(createProcessObject().domain).toEqual(aDomain); -}); diff --git a/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index e04f48a5a3b5..69bf384e713c 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -79,7 +79,7 @@ function createProcessEnv(): NodeJS.ProcessEnv { return Object.assign(proxy, process.env); } -export default function() { +export default function () { const process = require('process'); const newProcess = deepCyclicCopy(process, { blacklist: BLACKLIST, @@ -110,7 +110,7 @@ export default function() { } newProcess.env = createProcessEnv(); - newProcess.send = () => {}; + newProcess.send = () => { }; Object.defineProperty(newProcess, 'domain', { get() { From 9c21677373528f86b56c2e6c1b7a9028b3a37e97 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Mon, 11 Nov 2019 11:31:01 +0200 Subject: [PATCH 06/10] add missing space in readme --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 082758973f32..aa444d4f5c9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -49,7 +49,7 @@ - `[jest-snapshot]` [**BREAKING**] Distinguish empty string from internal snapshot not written ([#8898](https://github.com/facebook/jest/pull/8898)) - `[jest-snapshot]` [**BREAKING**] Remove `report` method and throw matcher errors ([#9049](https://github.com/facebook/jest/pull/9049)) - `[jest-transform]` Properly cache transformed files across tests ([#8890](https://github.com/facebook/jest/pull/8890)) -- `[jest-utils]`Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136)) +- `[jest-utils]` Allow querying process.domain ([#9136](https://github.com/facebook/jest/pull/9136)) - `[jest-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058)) ### Chore & Maintenance From 018cbab20844b83dc55a17cf4d0a9d97a01162d8 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Mon, 11 Nov 2019 11:33:21 +0200 Subject: [PATCH 07/10] fix lint --- jest.config.js | 5 ++++- .../jest-util/src/__tests__/createProcessObject.test.ts | 6 +++--- packages/jest-util/src/createProcessObject.ts | 4 ++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/jest.config.js b/jest.config.js index 9abbf1248713..35a6c8c613ff 100644 --- a/jest.config.js +++ b/jest.config.js @@ -63,5 +63,8 @@ module.exports = { transform: { '^.+\\.[jt]sx?$': '/packages/babel-jest', }, - watchPlugins: ['jest-watch-typeahead/filename', 'jest-watch-typeahead/testname'], + watchPlugins: [ + 'jest-watch-typeahead/filename', + 'jest-watch-typeahead/testname', + ], }; diff --git a/packages/jest-util/src/__tests__/createProcessObject.test.ts b/packages/jest-util/src/__tests__/createProcessObject.test.ts index bea547f0d19f..c1658f793983 100644 --- a/packages/jest-util/src/__tests__/createProcessObject.test.ts +++ b/packages/jest-util/src/__tests__/createProcessObject.test.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import { EventEmitter } from 'events'; +import {EventEmitter} from 'events'; let createProcessObject; @@ -42,7 +42,7 @@ it('fakes require("process") so it is equal to "global.process"', () => { }); it('checks that process.env works as expected on Linux platforms', () => { - Object.defineProperty(process, 'platform', { get: () => 'linux' }); + Object.defineProperty(process, 'platform', {get: () => 'linux'}); requireCreateProcessObject(); // Existing properties inside process.env are copied to the fake environment. @@ -81,7 +81,7 @@ it('checks that process.env works as expected on Linux platforms', () => { }); it('checks that process.env works as expected in Windows platforms', () => { - Object.defineProperty(process, 'platform', { get: () => 'win32' }); + Object.defineProperty(process, 'platform', {get: () => 'win32'}); requireCreateProcessObject(); // Windows is not case sensitive when it comes to property names. diff --git a/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index 69bf384e713c..e04f48a5a3b5 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -79,7 +79,7 @@ function createProcessEnv(): NodeJS.ProcessEnv { return Object.assign(proxy, process.env); } -export default function () { +export default function() { const process = require('process'); const newProcess = deepCyclicCopy(process, { blacklist: BLACKLIST, @@ -110,7 +110,7 @@ export default function () { } newProcess.env = createProcessEnv(); - newProcess.send = () => { }; + newProcess.send = () => {}; Object.defineProperty(newProcess, 'domain', { get() { From b49b0ba8855172bbc9485f5a0b03a572b4c01a08 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Mon, 11 Nov 2019 11:48:16 +0200 Subject: [PATCH 08/10] add facebook copyrights to test fixtures - setup.js --- e2e/create-process-object/setup.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/e2e/create-process-object/setup.js b/e2e/create-process-object/setup.js index 7b3df08a2655..5dd88ea88dc3 100644 --- a/e2e/create-process-object/setup.js +++ b/e2e/create-process-object/setup.js @@ -1,3 +1,10 @@ +/** + * 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. + */ + // Jest creates a copy of the global 'process' object and uses it instead of the one provided by node. // Since 'require'-ing the 'domain' module has a side-effect that modifies "process" to make it work with domains, // 'domain' has to be required before Jest performs the copy, i.e. in the global-setup phase represented by this file. From 8168c0e3d2dcd04e68ab2ed2c80c2a2dfce17d64 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Mon, 11 Nov 2019 13:56:15 +0200 Subject: [PATCH 09/10] only add domain when its not enumerable --- packages/jest-util/src/createProcessObject.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index e04f48a5a3b5..ebd2d1a7cb61 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -112,11 +112,17 @@ export default function() { newProcess.env = createProcessEnv(); newProcess.send = () => {}; - Object.defineProperty(newProcess, 'domain', { - get() { - return process.domain; - }, - }); + const domainPropertyDescriptor = Object.getOwnPropertyDescriptor( + newProcess, + 'domain', + ); + if (domainPropertyDescriptor && !domainPropertyDescriptor.enumerable) { + Object.defineProperty(newProcess, 'domain', { + get() { + return process.domain; + }, + }); + } return newProcess; } From a59ad646edf50a732abee43a813912087400ca85 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 11 Nov 2019 15:50:26 +0100 Subject: [PATCH 10/10] Update jest.config.js --- jest.config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jest.config.js b/jest.config.js index 35a6c8c613ff..a39a9f4789f5 100644 --- a/jest.config.js +++ b/jest.config.js @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +'use strict'; + module.exports = { collectCoverageFrom: [ '**/packages/*/**/*.js',