npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mocha-jsdom

v2.0.0

Published

Simple integration of jsdom into mocha tests

Downloads

118,302

Readme

Deprecation notice: Consider jsdom-global instead, a simpler alternative that also works outside of Mocha. mocha-jsdom still works, but jsdom-global is better supported.


mocha-jsdom

Test frontend libraries in the console using Node.js, mocha and jsdom.

Status

Usage

$ npm i --save-dev mocha-jsdom

npm version

Use jsdom() inside your describe(...) block (or the global context). It will turn your Node.js environment into a mock browser environment supporting the full DOM and browser API. The variables window, document, history (and so on) will then be available for use.

var jsdom = require('mocha-jsdom')
var expect = require('chai').expect

describe('mocha tests', function () {

  jsdom()

  it('has document', function () {
    var div = document.createElement('div')
    expect(div.nodeName).eql('DIV')
  })

})

See examples/basic for an example of a basic setup.

Upgrading to v2.0.0

If you are coming from mocha-jsdom v1.x, remove jsdom if you're not using it before upgrading. jsdom is now a direct dependency of mocha-jsdom.

# using Yarn
yarn remove jsdom
yarn upgrade mocha-jsdom
# using npm
npm uninstall -S -D jsdom
npm upgrade mocha-jsdom

Node and io.js information

As of jsdom 4.0.0, jsdom now requires io.js and will not work with Node.js 0.12 or below.

How it works

mocha-jsdom is a simple glue to integrate jsdom to mocha.

Invoking jsdom() will inject before and after handlers to the current mocha suite which will setup and teardown jsdom. Here's what it does:

  • Window: global.window will be available as the jsdom.

  • Globals: global variables like document and history are propagated, and they're cleaned up after tests run.

  • Error handling: jsdom errors are sanitized so that their stack traces are shortened.

NB: Before you try this library, learn about jsdom first. In fact, you may be able to integrate jsdom into your tests without this library; this is mostly syntactic sugar and reasonable defaults.

Using with a library

Perfect for testing small DOM-consuming utilities in the console. See test/jquery.js for an example.

describe('mocha tests', function () {

  var $
  jsdom()

  before(function () {
    $ = require('jquery')
  })

  it('works', function () {
    document.body.innerHTML = '<div>hola</div>'
    expect($("div").html()).eql('hola')
  })

})

See examples/basic for an example of a basic setup.

Using with a library, alternate

You can also pass the source code via src:

describe('mocha tests', function () {
  jsdom({
    src: fs.readFileSync('jquery.js', 'utf-8')
  })

  ...
})

Configuration

You can pass jsdom options:

describe('mocha tests', function () {
  jsdom({
    parsingMode: 'xml'
  })

  ...
})

Working with mocha --watch

When using with --watch, you my encounter strange errors from 3rd-party libraries like jQuery not working properly.

In these cases, use require('mocha-jsdom').rerequire instead of require(). This will ensure that the require() call will always happen.

var $
var jsdom = require('mocha-jsdom')
var rerequire = jsdom.rerequire

jsdom()

before(function () {
  $ = rerequire('jquery')
})

Special config

Other mocha-jsdom specific options:

  • globalize - propagates to values in window to global. defaults to true.

  • console - allows you to use console.log inside a jsdom script. defaults to true.

  • useEach - bind to Mocha's beforeEach/afterEach rather than before/after. defaults to false.

  • skipWindowCheck - skips checking of window at startup. When false, mocha-jsdom will throw an error if window already exists. Defaults to false.

Testling support

Yes, fully compatible with testling. A test suite using jsdom should be able to use testling.

See examples/basic for a setup that allows for testing via iojs (jsdom), testling, and mocha via the browser.

Thanks

mocha-jsdom © 2014-2018 Rico Sta. Cruz. Released under the MIT License. Authored and maintained by Rico Sta. Cruz with help from contributors (list).

      ricostacruz.com