From 70546e07a1d09c6067b413e64a6f571932a45641 Mon Sep 17 00:00:00 2001 From: Idan Attias Date: Mon, 11 Nov 2019 16:50:55 +0200 Subject: [PATCH] jest-utils: add a getter for process.domain (#9136) --- CHANGELOG.md | 1 + e2e/__tests__/createProcessObject.test.ts | 14 ++++++++++++++ .../__tests__/createProcessObject.test.js | 14 ++++++++++++++ e2e/create-process-object/package.json | 6 ++++++ e2e/create-process-object/setup.js | 14 ++++++++++++++ packages/jest-util/src/createProcessObject.ts | 12 ++++++++++++ 6 files changed, 61 insertions(+) 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 create mode 100644 e2e/create-process-object/setup.js diff --git a/CHANGELOG.md b/CHANGELOG.md index ebf99b7e814e..03bb90837c44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,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-transform]` Don't fail the test suite when a generated source map is invalid ([#9058](https://github.com/facebook/jest/pull/9058)) ### Chore & Maintenance 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/e2e/create-process-object/setup.js b/e2e/create-process-object/setup.js new file mode 100644 index 000000000000..5dd88ea88dc3 --- /dev/null +++ b/e2e/create-process-object/setup.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. + */ + +// 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/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index 53d18dd43cb5..ebd2d1a7cb61 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -112,5 +112,17 @@ export default function() { newProcess.env = createProcessEnv(); newProcess.send = () => {}; + const domainPropertyDescriptor = Object.getOwnPropertyDescriptor( + newProcess, + 'domain', + ); + if (domainPropertyDescriptor && !domainPropertyDescriptor.enumerable) { + Object.defineProperty(newProcess, 'domain', { + get() { + return process.domain; + }, + }); + } + return newProcess; }