3.0.2 • Published 7 years ago

jsdom-global v3.0.2

Weekly downloads
293,274
License
MIT
Repository
github
Last release
7 years ago

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

Install

Requires jsdom.

npm install --save-dev --save-exact jsdom jsdom-global

Note

jsdom-global now requires jsdom v10 or above. If you need jsdom v9 and below, use the previous version (jsdom-global@2).

Usage

Just invoke it to turn your Node.js environment into a DOM environment.

require('jsdom-global')()

// you can now use the DOM
document.body.innerHTML = 'hello'

You may also pass parameters to jsdomGlobal() like so: require('jsdom-global')(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('jsdom-global')()

// do things

cleanup()

Tape

In tape, run it before your other tests.

require('jsdom-global')()

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 jsdom-global/register

Advanced: For finer control, you can instead add it via mocha's before and after hooks.

before(function () {
  this.jsdom = require('jsdom-global')()
})

after(function () {
  this.jsdom()
})

ES2015

If you prefer to use import rather than require, you might want to use jsdom-global/register instead. Place it on top of your other import calls.

import 'jsdom-global/register'
import React from 'react'
import jQuery from 'jquery'
// ...

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')()
    
    // ...do your tests here
  • Running it with smokestack:

    browserify test.js | smokestack          # run in a browser
    node test.js                             # or the console
    browserify test.js --no-bundle-external  # also works (but why bother?)
  • Running it with Babel (babelify or babel-cli):

    browserify test.js -t babelify | smokestack  # run in a browser (with babel)
    babel-node test.js                           # or the console

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

@sprucelabs/react-heartwood-components@modevel/cli@rock-kit/mocha-environment-jsdom@plurid/pluridoc-cli@startdt/cli-plugin-unit-mocha@temporg/mocha-environment-jsdom@startdt/plugin-unit-mochafragrans-clifrag-clipack-cli@kurtzgmbh/front-react-app@kartoffelgames/enviromentlib-vue-componentsnpm-test-ahweblib-nodelottie-web-nodelottie2apng@everything-registry/sub-chunk-1980gulp-jst-extend2gltfjsxgltfjsx-webphtml-to-xpathsgrsbet-frontendgpx-jsgpxparserjslib-plugin-unit-mochakaibaice-ui-reactitsa-react-serverleyatils-webbuilderhandsome-react@bentley/presentation-testing@antv/torch@bitcoin-computer/lib@blackblockrocks/minecraft-render@bojagi/cli@connectv/sdh@datayoga-io/node-g6@devlander/hooks@dolphin-ocean/cli-ui@exdj158/minecraft-render@groqz/sanity@htmlacademy/courses-checks@htmlacademy/deploy-utils@fromage/cli-plugin-unit-mocha@griddo/ax@jedwards1211/js-react-toolchain@jcoreio/js-react@bitbrother/flash-splashelectricfilterable-react-pagination-statusfilterable-react-pagination-tableextension-cliextension-cli-gfwexport-contextgatsby-plugin-static-propsgatsby-plugin-noodlenduire-happo-testlakefirekushki-frontend-helpermyria-game-pvp-sdknuxt-test-utilsmu-enginepod32g-utilspfft-partial-mochapetrolpckg-app-frontend-devpm-extension-climount-hookmondlemokamokmerchant-dashboardmcore-extpailingual-odata-model-generatorparsegraph-reactpixi-shimreact-komponentpyobsplotreact-pagination-statusreact-pagination-tablesanitysanity-codegen-2san-cli-plugin-unit-mochasan-cli-testsan-cli-mocha-testsanity-type-generatorrivet-josh-deletereact-tsreact-visual-regression-testrescape-apolloreusable-formsy-componentsrocktime-webpacktest-callbag-jsxticktock-jsts-x-unittorchjstopbetta-frontendunidocsrvlibrary
3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.1.1

7 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.7.0

8 years ago

1.6.2

8 years ago

1.6.1

8 years ago

1.6.0

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago