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
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
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.cleanup = require('global-jsdom')()
})
after(function () {
this.cleanup()
})
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
browserify
support is dropped - I have no way to test this andwebpack
started giving higher priority to thebrowser
field inpackage.json
thanmodule
global.jsdom
will now be populated with the currentjsdom
instance - if you were using the originalmocha
instructions forglobal-jsdom
you may run into issues since you're potentially overwriting the globaljsdom
property. To work around just use a different name (see above).
Thanks
original code forked from jsdom-global