[Bug]: make jsdom accessible to extending environments again
See original GitHub issueVersion
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:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
@SimenB PR: https://github.com/facebook/jest/pull/12232
curious what’s changed that makes this possible now? 😃
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.