jsdom-global
Enables DOM in Node.js
jsdom-global will inject document
, window
and other DOM API into your Node.js environment. Useful for running, in Node.js, tests that are made for browsers.
![Status](https://travis-ci.org/rstacruz/jsdom-global.svg?branch=master)
Install
Requires jsdom.
npm install --save-dev --save-exact jsdom jsdom-global
Usage
Just invoke it to turn your Node.js environment into a DOM environment.
require('jsdom-global')()
document.body.innerHTML = 'hello'
To clean up after itself, just invoke the function it returns.
var cleanup = require('jsdom-global')()
cleanup()
You can also invoke it with a function block so it'll clean up afterwards.
var jsdom = require('jsdom-global')
jsdom(function () {
var $ = require('jquery')
$('body').html('hello')
})
Tape
In tape, run it before your other tests.
require('jsdom-global')()
test('your tests', (t) => {
})
Mocha
Just add it to mocha's before
and after
hooks.
before(function () {
this.jsdom = require('jsdom-global')()
})
after(function () {
this.jsdom()
})
Browserify
If you use Browserify on your tests (eg: smokestack, tape-run, budo, hihat, zuul, and so on), doing require('jsdom-global')()
is a noop. In practice, this means you can use jsdom-global even if your tests are powered by browserify, and your test will now work in both the browser and Node.
-
Writing your tests (test.js
):
require('jsdom-global')()
-
Running it with smokestack:
browserify test.js | smokestack
node test.js
browserify test.js --no-bundle-external
-
Running it with Babel (babelify or babel-cli):
browserify test.js -t babelify | smokestack
babel-node test.js
Thanks
jsdom-global © 2016+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).
ricostacruz.com ·
GitHub @rstacruz ·
Twitter @rstacruz