JSPM

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

Enable DOM in Node.js

Package Exports

  • global-jsdom

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (global-jsdom) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

global-jsdom

npm version

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 prefer to use import rather than require, you might want to use global-jsdom/register instead. Place it on top of your other import calls.

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