- Notifications
You must be signed in to change notification settings - Fork19
Simple jsdom integration with mocha
License
rstacruz/mocha-jsdom
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Deprecation notice:Considerjsdom-global instead, a simpler alternative that also works outside of Mocha.
mocha-jsdomstill works, butjsdom-globalis better supported.
Test frontend libraries in the console using Node.js,mocha andjsdom.
$ npm i --save-dev mocha-jsdom
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.
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-jsdomAs of jsdom 4.0.0,jsdom now requires io.js and will not work with Node.js 0.12 or below.
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.windowwill be available as the jsdom.Globals: global variables like
documentandhistoryare 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.
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.
You can also pass the source code viasrc:
describe('mocha tests',function(){jsdom({src:fs.readFileSync('jquery.js','utf-8')})...})
You can pass jsdom options:
describe('mocha tests',function(){jsdom({parsingMode:'xml'})...})
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')})
Other mocha-jsdom specific options:
globalize- propagates to values inwindowtoglobal. defaults to true.console- allows you to useconsole.loginside a jsdom script. defaultsto true.useEach- bind to Mocha'sbeforeEach/afterEachrather thanbefore/after.defaults to false.skipWindowCheck- skips checking ofwindowat startup. When false,mocha-jsdom will throw an error ifwindowalready exists. Defaults to false.
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.
mocha-jsdom © 2014-2018 Rico Sta. Cruz. Released under theMIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).
About
Simple jsdom integration with mocha
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors6
Uh oh!
There was an error while loading.Please reload this page.