Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-utils: add a getter for process.domain #9136

Merged
merged 11 commits into from Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -49,6 +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-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;
}