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

[Bug]: make jsdom accessible to extending environments again #12233

Closed
robin-drexler opened this issue Jan 13, 2022 · 7 comments · Fixed by #12232
Closed

[Bug]: make jsdom accessible to extending environments again #12233

robin-drexler opened this issue Jan 13, 2022 · 7 comments · Fixed by #12232

Comments

@robin-drexler
Copy link
Contributor

robin-drexler commented Jan 13, 2022

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
@robin-drexler robin-drexler changed the title [Bug]: make jsdom accessible to extending environments [Bug]: make jsdom accessible to extending environments again Jan 13, 2022
@robin-drexler
Copy link
Contributor Author

I thought about opening a PR and just making this.dom public again, but I assume that this would re-introduce the issue that @SimenB's PR fixed in the first place. So I closed it again.

I'm not sure what the best way forward here is, but would be open to help out if someone could give some guidance? 🙇

@SimenB
Copy link
Member

SimenB commented Jan 13, 2022

We can do this for Jest 28 when we'll stop shipping the jsdom env by default (so any types it pulls it won't affect people who don't use it).

I fixed the issue upstream anyways: DefinitelyTyped/DefinitelyTyped#57432

@robin-drexler
Copy link
Contributor Author

For anyone stumbling upon this:

@ts-ignore-ing seems to fix this issue for the time being.

// @ts-ignore
this.global.jsdom = this.dom;

@SimenB
Copy link
Member

SimenB commented Feb 10, 2022

@robin-drexler feel free to send a PR reverting #12107 🙂

@robin-drexler
Copy link
Contributor Author

robin-drexler commented Feb 10, 2022

@SimenB PR: #12232

curious what's changed that makes this possible now? :)

@SimenB
Copy link
Member

SimenB commented Feb 10, 2022

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants