Gathering detailed insights and metrics for jest-environment-jsdom-global
Gathering detailed insights and metrics for jest-environment-jsdom-global
Gathering detailed insights and metrics for jest-environment-jsdom-global
Gathering detailed insights and metrics for jest-environment-jsdom-global
A Jest environment that allows you to configure jsdom
npm install jest-environment-jsdom-global
v4.0.0: Jest 29 and EOL for Node 12
Published on 30 Aug 2022
v3.1.2: Bug fix
Published on 20 May 2022
v3.1.1: README updates
Published on 30 Apr 2022
v3.1.0: Jest 28 support
Published on 30 Apr 2022
v3.0.0: Jest 27 support + general hygiene
Published on 27 Aug 2021
v2.0.4: Update docs for JSDOM 16
Published on 14 Jun 2020
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
112 Stars
87 Commits
13 Forks
2 Watching
23 Branches
5 Contributors
Updated on 05 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-13.1%
23,450
Compared to previous day
Last week
-2.6%
122,887
Compared to previous week
Last month
10%
533,470
Compared to previous month
Last year
17.4%
6,123,404
Compared to previous year
1
3
Similar to the standard
jest-environment-jsdom
, but exposesjsdom
so that you can reconfigure it from your test suites.
For more information, see this discussion in the Jest repository.
Before installing, please check if you need this package, particularly in light of changes in Jest 28.
Install the package with yarn
:
1yarn add --dev jest-environment-jsdom-global jest-environment-jsdom
or npm
:
1npm install --save-dev jest-environment-jsdom-global jest-environment-jsdom
Then, add it to your Jest configuration:
1"jest": { 2 "testEnvironment": "jest-environment-jsdom-global" 3}
For more information, see the Jest documentation on testEnvironment
.
You can access the jsdom
object globally in your test suite. For example, here's a test that changes the URL for your test environment (using reconfigure
):
1describe("test suite", () => { 2 it("should not fail", () => { 3 jsdom.reconfigure({ 4 url: "https://www.example.com/", 5 }); 6 }); 7});
As of Jest 28 (formal docs), you can provide options to JSDOM using inline comments in your tests. For example, to set the URL, you could write a test suite that looks like this:
/**
* @jest-environment jsdom
* @jest-environment-options {"url": "https://jestjs.io/"}
*/
test('use jsdom and set the URL in this test file', () => {
expect(window.location.href).toBe('https://jestjs.io/');
});
This may solve for many previous use cases for this package, and I'd suggest using this approach rather than using jest-environment-jsdom-global
if it suffices for your needs.
Object.defineProperty
?Jest's browser environment is based on JSDOM. JSDOM used to allow you to use Object.defineProperty
to update certain properties on window
; in particular, you could change parts of window.location
, or window.top
, as you need to.
However, several years ago, JSDOM's API changed; the preferred way to mock window.location
and its child properties is to use reconfigure
. JSDOM 11 became the default back in Jest 22; as a result, tests that used Object.defineProperty
may no longer work on certain properties of window
.
Currently, Jest does not expose the JSDOM reconfigure
method inside test suites. The jest-environment-jsdom-global
package is meant to solve this problem: it adds jsdom
as a global, so you can reconfigure it within your tests.
window.location.href
?In your test, you can set the URL using:
1jsdom.reconfigure({ 2 url: "https://www.example.com/", 3});
window.location.hash
?You need to provide a full URL, not just the hash. Similarly to above, you can do:
1jsdom.reconfigure({ 2 url: "https://www.example.com/#myHash", 3});
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/11 approved changesets -- score normalized to 1
Reason
9 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More