npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jest-environment-jsdom-global

v4.0.0

Published

> Similar to the standard [`jest-environment-jsdom`](https://github.com/facebook/jest/tree/main/packages/jest-environment-jsdom), but exposes `jsdom` so that you can reconfigure it from your test suites.

Downloads

512,525

Readme

Jest environment for a globally-exposed JSDOM

Similar to the standard jest-environment-jsdom, but exposes jsdom 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.

Installation and configuration

Install the package with yarn:

yarn add --dev jest-environment-jsdom-global jest-environment-jsdom

or npm:

npm install --save-dev jest-environment-jsdom-global jest-environment-jsdom

Then, add it to your Jest configuration:

"jest": {
  "testEnvironment": "jest-environment-jsdom-global"
}

For more information, see the Jest documentation on testEnvironment.

Using JSDOM in your test suite

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):

describe("test suite", () => {
  it("should not fail", () => {
    jsdom.reconfigure({
      url: "https://www.example.com/",
    });
  });
});

Frequently Asked Questions

Jest 28 update: is this package still useful?

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.

Why can't I use 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.

How can I mock window.location.href?

In your test, you can set the URL using:

jsdom.reconfigure({
  url: "https://www.example.com/",
});

How can I mock window.location.hash?

You need to provide a full URL, not just the hash. Similarly to above, you can do:

jsdom.reconfigure({
  url: "https://www.example.com/#myHash",
});