Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Simple jsdom integration with mocha

License

NotificationsYou must be signed in to change notification settings

rstacruz/mocha-jsdom

Repository files navigation

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


mocha-jsdom

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

Status


Usage

$ npm i --save-dev mocha-jsdom

npm version

Usejsdom() inside yourdescribe(...) block (or the global context). It willturn your Node.js environment into a mock browser environment supporting thefull DOM and browser API. The variableswindow,document,history (and soon) will then be available for use.

varjsdom=require('mocha-jsdom')varexpect=require('chai').expectdescribe('mocha tests',function(){jsdom()it('has document',function(){vardiv=document.createElement('div')expect(div.nodeName).eql('DIV')})})

Seeexamples/basic for an example of a basic setup.


Upgrading to v2.0.0

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

# using Yarnyarn remove jsdomyarn upgrade mocha-jsdom
# using npmnpm uninstall -S -D jsdomnpm 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 integratejsdom to mocha.

Invokingjsdom() will injectbefore andafter handlers to the currentmocha suite which will setup and teardown jsdom. Here's what it does:

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

  • Globals: global variables likedocument andhistory are propagated,and they're cleaned up after tests run.

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

NB: Before you try this library, learn about jsdom first. In fact, you may beable to integrate jsdom into your testswithout this library; this is mostlysyntactic sugar and reasonable defaults.


Using with a library

Perfect for testing small DOM-consuming utilities in the console. Seetest/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')})})

Seeexamples/basic for an example of a basic setup.


Using with a library, alternate

You can also pass the source code viasrc:

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-partylibraries like jQuery not working properly.

In these cases, userequire('mocha-jsdom').rerequire instead ofrequire().This will ensure that therequire() call will always happen.

var$varjsdom=require('mocha-jsdom')varrerequire=jsdom.rerequirejsdom()before(function(){$=rerequire('jquery')})

Special config

Other mocha-jsdom specific options:

  • globalize - propagates to values inwindow toglobal. defaults to true.

  • console - allows you to useconsole.log inside a jsdom script. defaultsto true.

  • useEach - bind to Mocha'sbeforeEach/afterEach rather thanbefore/after.defaults to false.

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


Testling support

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

Seeexamples/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 theMIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

    ricostacruz.com

About

Simple jsdom integration with mocha

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors6


[8]ページ先頭

©2009-2025 Movatter.jp