Oskar Szura
Oskar Szura

Reputation: 2559

Mocha 'Uncaught ReferenceError: window is not defined'

I'm just starting with JS unit testing and while every tests are going well (I've went with Mocha) I'm having a problem while it requires some 'document's' attributes, like:

var baseTag  = document.getElementsByTagName('base');

it gives me the following error:

Mocha 'Uncaught ReferenceError: document is not defined'

my test run command is:

mocha -u bdd test.js --reporter spec

now my question is, do I need some PhantomJS (or simillar tool) for testing when I need an acces for document's and DOM objects? Or I'm just opened for any advice how this should be resolved.

Upvotes: 5

Views: 8919

Answers (1)

Louis
Louis

Reputation: 151401

Yes, you need to use something like PhantomJS or jsdom so to run your code against some sort of DOM tree. By default Node does not provide a DOM tree because it is rather specific functionality that most applications don't need.

What solution you want to select really depends on the code you are testing. I've had good results with jsdom to test code that only needs to navigate the nodes in a DOM tree. There is certainly a point at which jsdom won't do it. I'm not sure where the limit is.

Upvotes: 5

Related Questions