question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
harelmocommented, Oct 24, 2018

Thanks for pointing me to that code @SimenB - it actually made me think about a workaround to make the process object have the domain-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:

module.exports = () => { require('domain') }

and use it as the global-setup file in my jest.config.js file:

module.exports = {
  globalSetup: `test/setup/global-setup.js`
}

Since require-ing the domain module has the side-effect on process 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.

0reactions
github-actions[bot]commented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Domains not properly catching errors while testing nodeJS in ...
Found the problem. NodeJS domains catch synchronous errors but the event continues to bubble to a try/catch. If you wrap a domain.run() in...
Read more >
A Node.js Guide to Actually Doing Integration Tests - Toptal
While we'll use JavaScript/Node.js for all code examples in this article, ... Broken database schema; Invalid cache integration; Flaws in business logic or ......
Read more >
Troubleshooting - Jest
Troubleshooting. Uh oh, something went wrong? Use this guide to resolve issues with Jest. Tests are Failing and You Don't Know Why​.
Read more >
jest-environment-node | Yarn - Package Manager
Fixes for global built in objects in jest-environment-node . Create mock objects in the vm context instead of the parent context. .babelrc is...
Read more >
Frontend testing standards and style guidelines - GitLab Docs
There are two types of test suites encountered while developing frontend code at GitLab. We use Jest for JavaScript unit and integration testing,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found