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.

[Bug]: make jsdom accessible to extending environments again

See original GitHub issue

Version

27.4.7

Steps to reproduce

I prepared a small reproduction repository. It uses a custom TS environment that tries to access this.dom and fails.

To see for yourself:

  • clone the repo
  • run npm install
  • run npx jest

Expected behavior

Jest Environments that extend jest-environment-jsdom should be able to access this.dom.

Basically, this should work:

export class JSDOMEnvironmentGlobal extends JSDOMEnvironment {
  constructor(config, options) {
    super(config, options);

   // this line triggers the error
    this.global.jsdom = this.dom;
  }
}

Actual behavior

That doesn’t work if the project uses TypeScript because this.dom has recently been made private.

The error you get if you try to access this.dom in an environment:

 Property 'dom' is private and only accessible within class 'JSDOMEnvironment'.

Additional context

jest-environment-jsdom-global (a popular npm package that uses the same technique to make jsdom available) is currently unaffected because it uses JS and the transpiled CommonJS version of jest-environment-jsdom doesn’t enforce this.dom to be private. If jest changes its bundling process in the future, this could also fail for projects using JS.

I think it’d be great if jsdom continued to be accessible for custom environments since that enables use cases that would otherwise not be possible.

Environment

System:
    OS: macOS 12.1
    CPU: (8) x64 Apple M1
  Binaries:
    Node: 14.16.1 - /usr/local/bin/node
    Yarn: 2.4.3 - ~/.npm-global/bin/yarn
    npm: 7.11.2 - /usr/local/bin/npm

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
robin-drexlercommented, Feb 10, 2022

@SimenB PR: https://github.com/facebook/jest/pull/12232

curious what’s changed that makes this possible now? 😃

0reactions
github-actions[bot]commented, Mar 13, 2022

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

jest-environment-jsdom | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >
`window` is not being exposed to Jest - Stack Overflow
However, I still get the error and it seems JSDOM's window object isn't exposed to the test. How can I properly expose global...
Read more >
From v27 to v28 - Jest
Upgrading Jest from v27 to v28? This guide aims to help refactoring your configuration and tests.
Read more >
How I Fixed The Unexpected Token Error In Jest
Test environment jest-environment-jsdom cannot be found. Make sure the testEnvironment configuration option points to an existing node module.
Read more >
Using LinkeDOM as test environment in Jest unit tests
Also I wasn't super familiar with the inner workings of Jest, JSDOM and React Testing Library, making this a great occasion to learn....
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