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
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 globalSetupFile.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.
idan-at marked this conversation as resolved.
Show resolved Hide resolved
// 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');
};
3 changes: 1 addition & 2 deletions jest.config.js
Expand Up @@ -5,8 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

'use strict';

module.exports = {
collectCoverageFrom: [
'**/packages/*/**/*.js',
Expand All @@ -20,6 +18,7 @@ module.exports = {
'!**/vendor/**',
'!e2e/**',
],
globalSetup: '<rootDir>/globalSetupFile.js',
modulePathIgnorePatterns: [
'examples/.*',
'packages/.*/build',
Expand Down
8 changes: 8 additions & 0 deletions packages/jest-util/src/__tests__/createProcessObject.test.ts
Expand Up @@ -6,6 +6,7 @@
*/

import {EventEmitter} from 'events';
import * as domain from 'domain';

let createProcessObject;

Expand Down Expand Up @@ -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();
Object.defineProperty(process, 'domain', {get: () => aDomain});

expect(createProcessObject().domain).toEqual(aDomain);
});
6 changes: 6 additions & 0 deletions packages/jest-util/src/createProcessObject.ts
Expand Up @@ -112,5 +112,11 @@ export default function() {
newProcess.env = createProcessEnv();
newProcess.send = () => {};

Object.defineProperty(newProcess, 'domain', {
get() {
return process.domain;
},
});

return newProcess;
}