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.

JSDOM virtualConsole environment option defunct in parallel mode

See original GitHub issue

šŸ› Bug Report

Overriding virtual console for jsdom isn’t possible for parallel execution.

To Reproduce

(Repl.it link further down.)

In any project using jest with at least two test files using jsdom, override the virtual console setting:

In jest.config.js:

    ...
    testEnvironment: "jsdom",
    testEnvironmentOptions: {
        virtualConsole: new require('jsdom').VirtualConsole()
    }
    ...

Then run:

  • jest

Expected behavior

The tests run as they should.

Actual behavior

The tests die with an error from JSDOM:

TypeError: options.virtualConsole must be a VirtualConsole (from createVirtualConsole)

(createVirtualConsole is a bug in JSDOM - it should say VirtualConsole AFAIK.)

This looks like a bug in the way Jest fans out test to workers, because if you runs sequentially, it works. To demonstrate this, try:

  • jest --runInBand

Link to repl or repo (highly encouraged)

https://repl.it/@mzedeler/Reproducing-virtualConsole-option-bug

Change first line in index.js to switch between passing and failing case.

Issues without a reproduction link are likely to stall.

Run npx envinfo --preset jest

Paste the results here:

  System:
    OS: Linux 4.15 Ubuntu 18.04.1 LTS (Bionic Beaver)
    CPU: (8) x64 Intel(R) Core(TM) i7-4810MQ CPU @ 2.80GHz
  Binaries:
    Node: 12.0.0 - ~/.nvm/versions/node/v12.0.0/bin/node
    Yarn: 1.15.2 - ~/.nvm/versions/node/v12.0.0/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v12.0.0/bin/npm
  npmPackages:
    jest: 24 => 24.7.1 

This is relate to https://github.com/facebook/jest/issues/5223.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
SimenBcommented, May 6, 2019

The bug here is that it works in run-in-band, btw, so I don’t think you’d like the fix šŸ˜… The way parallelized tests work is by running separate processes for them. The only way to communicate between processes is to pass serializable messages (in essence, we do JSON.stringify). This is done on config, so when you do new VirtualConsole, that’s not gonna be sent across, leading to the error you’re seeing in JSDOM. Additionally, as probably noted, when you do new something in the config, that instance is shared across all tests, which you probably don’t want.

I’ll close this as the solution to your issue is to create a custom environment or just stub out console as you noted. We’ll address config silently dropping fields as part of #7185

@SimenB @jeysal Should we add an option for virtualConsole field of testEnvironmentOptions is a function to call in the setup?

See above - the solution in this case is a custom env - we currently do not support non-serializable things (like functions) in the config

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

How to use the jsdom.VirtualConsole function in jsdom - Snyk
To help you get started, we've selected a few jsdom.VirtualConsole examples, based on popular ways it is used in public projects.
Read more >
jest-environment-jsdom | Yarn - Package Manager
Jest uses verbose mode when running a single test file. Console messages are now buffered and printed along with the test results. Fix...
Read more >
Boyan Georgiev / jsdom-with-fix | GitLab
createDocument()` to create a document with its parsing mode set to ... option to `jsdom.jsdom()` or `jsdom.env()`, the given string is nowĀ ...
Read more >
node.js - New JSdom API - errors - Stack Overflow
jsdom's new API automatically creates a virtual console for you that's piped to the real (node's) console. Previous versions of jsdom usedĀ ...
Read more >
Jsdom: JavaScript implementation of HTML and the DOM
The jsdom sandbox is not foolproof, and code running inside the DOM's <script> s can, if it tries hard enough, get access to...
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