admin管理员组文章数量:1327301
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 mand 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.
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 mand 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.
Share Improve this question asked Dec 12, 2013 at 11:59 Oskar SzuraOskar Szura 2,5695 gold badges34 silver badges43 bronze badges 3- Have you tried window.document.getElementsByTagName()? – Bora Commented Dec 12, 2013 at 13:21
- It won't help cause neither window neither document doesn't exist. As far for now I came up with the idea to 'mock-up' this object. – Oskar Szura Commented Dec 12, 2013 at 14:27
- You can use something like mocha-phantomjs, but the easiest way in the long run is to use Karma to run your mocha tests. It will fire up the browsers and run your code inside those browser environments. You can also use it headless and just use PhantomJS. – oligofren Commented Dec 11, 2014 at 9:07
1 Answer
Reset to default 5Yes, 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.
本文标签: javascriptMocha 39Uncaught ReferenceError window is not defined39Stack Overflow
版权声明:本文标题:javascript - Mocha 'Uncaught ReferenceError: window is not defined' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742204652a2432589.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论