Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

global-jsdom

modosc446.2kMIT26.0.0TypeScript support: included

Enable DOM in Node.js

dom, jsdom, test

readme

global-jsdom

npm version Node.js CI

Enables DOM in Node.js global-jsdom will inject document, window and other DOM API into your Node.js environment. This allows you to run browser tests in Node.js. The specific attributes set on global come directly from the jsdom version you have installed.

Versioning

global-jsdom uses the same major version as the jsdom that it wraps.

Install

Requires node >= 18.

npm install --save-dev --save-exact jsdom global-jsdom

Usage

Just invoke it to turn your Node.js environment into a DOM environment.

// commonjs
require('global-jsdom/register')

// or es2015
import 'global-jsdom/register'

// you can now use the DOM
document.body.innerHTML = 'hello'

// you can also access the current jsdom instance through $jsdom
global.$jsdom.reconfigure({})

Configuration

You may pass configuration parameters to jsdom like so:

// commonjs
const globalJsdom = require('global-jsdom')

// or es2015
import globalJsdom from 'global-jsdom'

// then
globalJsdom(html, options)

Check the jsdom.jsdom() documentation for valid values for the options parameter.

Default Options

The following set of default options are passed to jsdom

{
  // if url isn't set then localStorage breaks with a cryptic error, see
  // https://github.com/jsdom/jsdom/issues/2304#issuecomment-408320484
  url: 'http://localhost:3000',
  // pretendToBeVisual is enabled so that react works, see
  // https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
  pretendToBeVisual: true,
}

Cleanup

To clean up the global namespace just invoke the returned function:

// commonjs
const cleanup = require('global-jsdom')()

// es2015
import globalJsdom from 'global-jsdom'
const cleanup = globalJsdom()

// do things, then
cleanup()

Tape

In tape, run it before your other tests.

require('global-jsdom/register')

test('your tests', (t) => {
  /* and so on... */
})

Mocha

Simple: Use Mocha's --require option. Add this to the test/mocha.opts file (create it if it doesn't exist)

-r global-jsdom/register

Advanced: For finer control, you can instead add it via mocha's before and after hooks.

before(function () {
  this.jsdom = require('global-jsdom')()
})

after(function () {
  this.jsdom()
})

ES2015

If you're using a recent version of node then import should just work:

import 'global-jsdom/register'
import React from 'react'
import jQuery from 'jquery'
// ...

Typescript

The library includes automatic support providing the necessary type declarations for an integration without further configuration.

import globalJsdom from "global-jsdom";

describe("Typescript test example", () => {
  let cleanup: { (): void };

  before(() => {
      cleanup = globalJsdom();
  });

  after(() => {
    cleanup();
  });

})

Thanks

original code forked from jsdom-global

changelog

[26.0.0] - {2025-01-10}

  • Raise minimum jsdom peer dependency to v26

[25.0.0] - {2024-09-17}

  • Raise minimum jsdom peer dependency to v25

[24.0.0] - {2024-01-27}

  • Peg major version to jsdom major version
  • Raise minimum required node version to v18

[9.2.0] - {2023-12-10}

[9.1.0] - {2023-08-06}

[9.0.1] - {2023-05-03}

  • Add README.md to global-jsdom package directory

[9.0.0] - {2023-05-03}

  • Raise minimum jsdom peer dependency to v22
  • Raise minimum required node version to v16

[8.8.0] - {2023-03-20}

[v8.7.0]

  • Relax jsdom peer dependency for jsdom 21
  • update deps

[v8.6.0]

[v8.5.0]

  • Relax jsdom peer dependency for jsdom 20
  • Update deps

[v8.4.0]

  • Relax jsdom peer dependency for jsdom 19
  • Update deps

[v8.3.0]

  • Relax jsdom peer dependency for jsdom 18
  • Update deps

[v8.2.0]

  • Relax jsdom peer dependency for jsdom 17

[v8.1.0]

[v8.0.0]

Feb 10, 2021

v5.0.0

Jul 14, 2019

  • Handle jsdom 15

v4.3.0

Jul 14, 2019

  • Update deps

v4.2.0

Oct 19, 2017

  • Just kidding, use global.$jsdom instead.

v4.1.0

Oct 18, 2017

  • Don't clobber global.jsdom, use global._jsdom instead

[v8.3.0]

  • Relax jsdom peer dependency for jsdom 18
  • Update deps## v4.0.0

    Oct 18, 2017

  • Update peerDeps for jsdom 11

  • Add es module export
  • Drop browserify support
  • Add access to global.jsdom instance

v3.0.2

May 8, 2017

v3.0.0

May 8, 2017

v2.1.1

Dec 24, 2016

  • #11 - Fix issues with XMLHttpRequest.

v2.1.0

Aug 22, 2016

  • #6, #7 - Fix support for jsdom 9.4.0.

v2.0.0

May 13, 2016

  • #3 - Allow overriding html and options being passed to jsdom. (#5, @kenjiru)
  • Deprecate the undocumented (and never-used) feature of calling jsdom(function).

v1.7.0

Mar 21, 2016

  • Implement jsdom-global/register for use in simpler cases.

v1.6.2

Feb 22, 2016

  • Fix typo in browser.js.

v1.6.1

Jan 15, 2016

  • Make jsdomGlobal() idempotent - that is, you may call it twice and expect the same result without any side effects.

v1.5.0

Jan 12, 2016

  • Remove tape integration... we don't need it.

v1.4.0

Jan 12, 2016

  • tape: Shows navigator userAgent in tape output.

v1.3.0

Jan 11, 2016

  • Add browserify support.

v1.2.0

Jan 11, 2016

  • Fix compatibility with legacy Node.js versions.

v1.1.0

Jan 11, 2016

  • Add cleanup().

v1.0.0

Jan 11, 2016

  • Initial release.