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

with-dom-overwrites

v1.2.1

Published

A utility for easily stubbing / overwriting properties of the jsdom instance (at any nesting level) in jest tests

Downloads

73

Readme

with-dom-overwrites

CircleCI

with-dom-overwrites is a utility for easily stubbing / overwriting properties of the jsdom instance (at any nesting level) in jest tests. In other words, it allows for the ability to do this:

import withDomOverwrites from 'with-dom-overwrites'

describe('some document context', () => {
  it('should work', () => {
    withDomOverwrites({
      overwrites: {
        'window.top': {},
        'window.custom.prop': 'custom prop',
        'document.referrer': 'foo',
        'document.location.href': 'https://www.google.com',
        'document.documentElement.outerHTML': `
          <html data-foo="foo..">
            <body>
              <h1>Hello World</h1>
            </body>
          </html>
        `,
      },
      run: () => {
        expect(window.top).toEqual({})
        expect(window.custom.prop).toBe('custom prop')
        expect(document.referrer).toBe('foo')
        expect(document.location.href).toBe('https://www.google.com')
        expect(document.documentElement.getAttribute('data-foo')).toBe('foo..')
      }
    })
  })
})

Instead of having to do this:

import jsdom from 'jsdom';

describe('some scenario that happens in the browser', () => {
  it('should work', () => {
    const url = 'https://www.google.com'
    const windowTop = {}
    const referrer = 'foo'
    const html = `
      <html data-foo="foo..">
        <body>
          <h1>Hello World</h1>
        </body>
      </html>
    `
    const dom = new jsdom.JSDOM(html);
    const originalDocument = global.document;

    Object.defineProperty(global, 'window', {
      value: dom.window,
      writable: true
    });

    Object.defineProperty(global, 'document', {
      value: dom.window.document,
      writable: true
    });

    Object.defineProperty(window.document, 'referrer', {
      value: referrer,
      writable: true
    });    

    window.custom = { prop: 'custom prop' }
    
    dom.reconfigure({ url, windowTop });
 
    expect(window.top).toBe(windowTop)
    expect(window.custom.prop).toBe('custom prop)
    expect(document.referrer).toBe(referrer)
    expect(document.location.href).toBe(url)
    expect(document.documentElement.getAttribute('data-foo')).toBe('foo..')

    global.document = originalDocument;
  })
})

Installation

npm install with-dom-overwrites --save-dev

Motivation

Though it is easy to access the jsdom window / document object within a jest test, it's not always easy to manipulate them. For instance, trying to set the document URL by supplying a value to the document.location.href property will not work. Rather, to change the URL, one will have to use the jsdom.reconfigure method, which requires a custom jsdom environment in order to be used within a jest test. The same goes for setting the window.top property, and there are one or two other scenarios that require a custom jsdom environment. This is not ideal as not only is it a bit cumbersome to setup each time, all the steps required make it something that is easily forgotten. But this is not the only issue. Even with the things that can be changed directly on the global window object, changing them is not always straight forward as a lot of things require Object.defineProperty to be changed, which does not lead to the most concise code.

with-dom-overwrites aims to provide a single utility withDomOverwrites({...}) function that can be used to easily and uniformly overwrite values on the jsdom instance at any nesting level, while ensuring that the original jsdom instance is restored afterwards.