All Projects → rstacruz → Jsdom Global

rstacruz / Jsdom Global

Enable DOM in Node.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Jsdom Global

Browser Monkey
Reliable DOM testing
Stars: ✭ 53 (-87.7%)
Mutual labels:  mocha, dom
Wheels
以自己是否能造出轮子来衡量学习的效果
Stars: ✭ 406 (-5.8%)
Mutual labels:  dom
Plain Draggable
The simple and high performance library to allow HTML/SVG element to be dragged.
Stars: ✭ 362 (-16.01%)
Mutual labels:  dom
Gulp Mocha
Run Mocha tests
Stars: ✭ 374 (-13.23%)
Mutual labels:  mocha
Highlighter.js
Easily navigate the DOM and highlight the elements - http://720kb.github.io/highlighter.js/
Stars: ✭ 370 (-14.15%)
Mutual labels:  dom
Enzyme
JavaScript Testing utilities for React
Stars: ✭ 19,781 (+4489.56%)
Mutual labels:  mocha
Pg Mem
An in memory postgres DB instance for your unit tests
Stars: ✭ 350 (-18.79%)
Mutual labels:  mocha
Dom
DOM library for Go and WASM
Stars: ✭ 424 (-1.62%)
Mutual labels:  dom
Vue Skeleton Mvp
VueJs, Vuetify, Vue Router and Vuex skeleton MVP written on JavaScript using async/await built to work with API REST skeleton: https://github.com/davellanedam/node-express-mongodb-jwt-rest-api-skeleton
Stars: ✭ 406 (-5.8%)
Mutual labels:  mocha
Practicalnode
Practical Node.js, 1st and 2nd Editions [Apress] 📓
Stars: ✭ 3,694 (+757.08%)
Mutual labels:  mocha
Anglesharp
👼 The ultimate angle brackets parser library parsing HTML5, MathML, SVG and CSS to construct a DOM based on the official W3C specifications.
Stars: ✭ 4,018 (+832.25%)
Mutual labels:  dom
Grunt Mocha
[MOVED] Grunt task for running mocha specs in a headless browser (PhantomJS)
Stars: ✭ 371 (-13.92%)
Mutual labels:  mocha
Eslint Plugin Testing Library
ESLint plugin to follow best practices and anticipate common mistakes when writing tests with Testing Library
Stars: ✭ 384 (-10.9%)
Mutual labels:  dom
Html Dom
This project is developed by Nguyen Huu Phuoc. I love building products and sharing knowledge.
Stars: ✭ 4,269 (+890.49%)
Mutual labels:  dom
Via.js
Write JS code that runs in a different context. E.g. use the DOM in a Web Worker.
Stars: ✭ 412 (-4.41%)
Mutual labels:  dom
Baretest
An extremely fast and simple JavaScript test runner.
Stars: ✭ 364 (-15.55%)
Mutual labels:  mocha
Fn Fx
A Functional API around JavaFX / OpenJFX.
Stars: ✭ 373 (-13.46%)
Mutual labels:  dom
React Hot Redux Firebase Starter
🌋 React + Redux + Firebase + Webpack + React Hot Loader 3 + React Router in one boilerplate
Stars: ✭ 375 (-12.99%)
Mutual labels:  mocha
Webappsec Trusted Types
A browser API to prevent DOM-Based Cross Site Scripting in modern web applications.
Stars: ✭ 424 (-1.62%)
Mutual labels:  dom
Hrjs
🔄 Tiny JavaScript plugin for highlighting and replacing text in the DOM
Stars: ✭ 420 (-2.55%)
Mutual labels:  dom

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 ([email protected]).

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].