Skip to content

Commit

Permalink
jest-utils: add a getter for process.domain (#9136)
Browse files Browse the repository at this point in the history
  • Loading branch information
idan-at authored and SimenB committed Nov 11, 2019
1 parent 77c17bc commit 70546e0
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions 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);
});
14 changes: 14 additions & 0 deletions 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();
});
});
6 changes: 6 additions & 0 deletions e2e/create-process-object/package.json
@@ -0,0 +1,6 @@
{
"jest": {
"testEnvironment": "node",
"globalSetup": "<rootDir>/setup.js"
}
}
14 changes: 14 additions & 0 deletions 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');
};
12 changes: 12 additions & 0 deletions packages/jest-util/src/createProcessObject.ts
Expand Up @@ -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;
}

0 comments on commit 70546e0

Please sign in to comment.