Node process.domain behavior broken when running code using Jest
See original GitHub issue🐛 Bug Report
Jest seems to modify the process
object in such a way that breaks the ability to run code inside a domain - i.e., process.domain
does not change to be the current domain set by calling the enter()
method of a domain or running an arbitrary code through the run()
method.
- I know this feature is deprecated in node, yet we still have some production-code using it heavily - so suggestions to stop using this modules are unfortunately invaluable at the moment.
To Reproduce
Steps to reproduce the behavior:
Run jest with the following spec file (not setup or actual production-code required):
const domain = require('domain')
describe('node domain', () => {
test('should show that the current domain is used when running a function using domain.run(...)', () => {
const d = domain.create()
d.run(() => {
expect(process.domain).not.toBeNull()
})
})
})
Expected behavior
The test above should pass, since process.domain
should be set to the actual domain object in which the code is being run. See https://nodejs.org/docs/latest-v8.x/api/domain.html#domain_domain_enter.
Running the test in mocha
+ chai
(with the relevant changes to the test only - requiring expect
from chai
and changing the matcher to not.to.be.null
) results in a passing test.
Link to repl or repo
https://repl.it/@HarelMoshe/testNodeDomains
Output from npx envinfo --preset jest
:
System:
OS: macOS High Sierra 10.13.2
CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Binaries:
Node: 8.12.0 - ~/.nvm/versions/node/v8.12.0/bin/node
Yarn: 1.6.0 - /usr/local/bin/yarn
npm: 5.10.0 - ~/.nvm/versions/node/v8.12.0/bin/npm
npmPackages:
jest: ^23.6.0 => 23.6.0
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Thanks for pointing me to that code @SimenB - it actually made me think about a workaround to make the
process
object have thedomain
-related code attached to it prior to Jest performing the copy. All I had to do is create a global-setup file exporting the following:test/setup/global-setup.js
:and use it as the global-setup file in my
jest.config.js
file:Since
require
-ing thedomain
module has the side-effect onprocess
in the global-setup phase - i.e., prior to Jest making its copy - that logic is persisted to the copy and everything seems to work fine.This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.