Package Exports
- global-jsdom
- global-jsdom/register
Readme
global-jsdom
Enables DOM in Node.js
global-jsdom
will injectdocument
,window
and other DOM API into your Node.js environment. This allows you to run browser tests in Node.js. The specific attributes set onglobal
come directly from thejsdom
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
browserify
support is dropped - I have no way to test this andwebpack
started giving higher priority to thebrowser
field inpackage.json
thanmodule
Thanks
original code forked from jsdom-global