JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 123077
  • Score
    100M100P100Q156284F
  • License MIT

Enable DOM in Node.js

Package Exports

  • global-jsdom
  • global-jsdom/register

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.

Install

Requires jsdom v10 or above.

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

Usage

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

require('global-jsdom')()

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

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

You may also pass parameters to globalJsdom() like so: require('global-jsdom')(html, options). Check the jsdom.jsdom() documentation for valid values for the options parameter.

To clean up after itself, just invoke the function it returns.

var cleanup = require('global-jsdom')()

// do things

cleanup()

Tape

In tape, run it before your other tests.

require('global-jsdom')()

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'
// ...

Migration from jsdom-global

  1. browserify support is dropped - I have no way to test this and webpack started giving higher priority to the browser field in package.json than module

Thanks

original code forked from jsdom-global