Giter Site home page Giter Site logo

global-jsdom's Introduction

global-jsdom

npm version Node.js CI

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 node >= 16 and jsdom >= 22 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.

// commonjs
require('global-jsdom/register')

// or es2015
import 'global-jsdom/register'

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

// you can also access the current jsdom instance through $jsdom
global.$jsdom.reconfigure({})

Configuration

You may pass configuration parameters to jsdom like so:

// commonjs
const globalJsdom = require('global-jsdom')

// or es2015
import globalJsdom from 'global-jsdom'

// then
globalJsdom(html, options)

Check the jsdom.jsdom() documentation for valid values for the options parameter.

Default Options

The following set of default options are passed to jsdom

{
  // if url isn't set then localStorage breaks with a cryptic error, see
  // https://github.com/jsdom/jsdom/issues/2304#issuecomment-408320484
  url: 'http://localhost:3000',
  // pretendToBeVisual is enabled so that react works, see
  // https://github.com/jsdom/jsdom#pretending-to-be-a-visual-browser
  pretendToBeVisual: true,
}

Cleanup

To clean up the global namespace just invoke the returned function:

// commonjs
const cleanup = require('global-jsdom')()

// es2015
import globalJsdom from 'global-jsdom'
const cleanup = globalJsdom()

// do things, then
cleanup()

Tape

In tape, run it before your other tests.

require('global-jsdom/register')

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'
// ...

Typescript

The library includes automatic support providing the necessary type declarations for an integration without further configuration.

import globalJsdom from "global-jsdom";

describe("Typescript test example", () => {
  let cleanup: { (): void };

  before(() => {
      cleanup = globalJsdom();
  });

  after(() => {
    cleanup();
  });

})

Thanks

original code forked from jsdom-global

global-jsdom's People

Contributors

attekemppila avatar coryhouse avatar dankaplanses avatar dependabot-preview[bot] avatar dependabot[bot] avatar ffluk3 avatar jtag05 avatar kenjiru avatar modosc avatar rstacruz avatar to-codando avatar willsoto avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.