npm

Join us for the upcoming webinar, "Enterprise JavaScript done right: the tools you love, the security you need."Sign up here »

global-jsdom

5.0.0 • Public • Published

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

Keywords

install

npm i global-jsdom

Downloadsweekly downloads

2,841

version

5.0.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability