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:
我刚开始使用JS单元测试,而且每次测试都很顺利(我已经和Mocha一起去了)我遇到了一个问题,虽然它需要一些'文档'属性,比如:
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.
现在我的问题是,当我需要访问文档和DOM对象时,是否需要一些PhantomJS(或simillar工具)进行测试?或者我只是打开任何建议如何解决这个问题。
1 个解决方案
#1
4
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.
是的,你需要使用像PhantomJS或jsdom这样的东西,以便针对某种DOM树运行你的代码。默认情况下,Node不提供DOM树,因为它是大多数应用程序不需要的特定功能。
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.
您想要选择哪种解决方案取决于您正在测试的代码。我已经用jsdom测试了只需要在DOM树中导航节点的代码。肯定有一点,jsdom不会这样做。我不确定限制在哪里。
#1
4
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.
是的,你需要使用像PhantomJS或jsdom这样的东西,以便针对某种DOM树运行你的代码。默认情况下,Node不提供DOM树,因为它是大多数应用程序不需要的特定功能。
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.
您想要选择哪种解决方案取决于您正在测试的代码。我已经用jsdom测试了只需要在DOM树中导航节点的代码。肯定有一点,jsdom不会这样做。我不确定限制在哪里。