使用Jasmine和node.js测试客户端javascript代码

时间:2022-05-25 16:14:44

Given: client-side javascript code (runs in browser, uses jquery etc). Currently the code is tested with Rhino and envjs. We would like to switch to node.js. However, after some research, couldn't find any envjs-like supplementary that emulates a browser.

给定:客户端javascript代码(在浏览器中运行,使用jquery等)。目前,代码使用Rhino和envjs进行测试。我们想切换到node.js.然而,经过一些研究,找不到任何模仿浏览器的类似envjs的补充。

While running node.js "as is", even basic capabilities like alert() or window are missing.

在“按原样”运行node.js时,甚至缺少alert()或window等基本功能。

Is there any standard bundle, similar to Rhino & envjs for node.js please?

是否有任何标准捆绑,类似于node.js的Rhino&envjs,请?

2 个解决方案

#1


9  

You could use zombie.js, which has everything you need for testing. Or you could leverage jsdom (which zombie.js uses internally) to get a DOM in node.js, and execute your tests against that DOM.

你可以使用zombie.js,它拥有测试所需的一切。或者您可以利用jsdom(zombie.js在内部使用)在node.js中获取DOM,并针对该DOM执行测试。

I can also recommend testling, which executes tests according to your specification in all common browsers -- the code is running in actual browsers against your service.

我还可以推荐testling,它在所有常见浏览器中根据您的规范执行测试 - 代码在实际浏览器中针对您的服务运行。

Here's a simple example with jsdom:

这是jsdom的一个简单示例:

var jsdom = require("jsdom");

jsdom.env(url, ["http://code.jquery.com/jquery.min.js"], function(err, window) {
    // jQuery is at window.$
});

Instead of url above, you could have an HTML document, or fragment.

您可以拥有HTML文档或片段,而不是上面的URL。

You can also load a page and fetch any external resources, instead of providing jQuery etc directly to jsdom:

您还可以加载页面并获取任何外部资源,而不是直接向jsdom提供jQuery等:

var jsdom = require("jsdom").jsdom,
    doc = jsdom(markup),
    window = doc.createWindow();

// Do your stuff on window, jsdom will have fetched all the scripts referenced in the markup

Again, zombie.js uses jsdom internally and it might be a better starting point.

同样,zombie.js在内部使用jsdom,它可能是一个更好的起点。

#2


2  

There are two options for this

有两种选择

  1. Your testing browser code. Run it in the browser. Emulating the browser doesn't proof your code works, at all.
  2. 您的测试浏览器代码。在浏览器中运行它。模拟浏览器并不能证明您的代码是有效的。

  3. Use a tool like phantom / zombie
  4. 使用像幻影/僵尸这样的工具

Of course there are alternatives to this, you can extract any non-browser related code, write a unit test suite for them and run it in node. It's just JavaScript.

当然还有其他选择,您可以提取任何非浏览器相关代码,为它们编写单元测试套件并在节点中运行它。这只是JavaScript。

You can also use managed services like testling to run your browser tests for you

您还可以使用像testling这样的托管服务来为您运行浏览器测试

#1


9  

You could use zombie.js, which has everything you need for testing. Or you could leverage jsdom (which zombie.js uses internally) to get a DOM in node.js, and execute your tests against that DOM.

你可以使用zombie.js,它拥有测试所需的一切。或者您可以利用jsdom(zombie.js在内部使用)在node.js中获取DOM,并针对该DOM执行测试。

I can also recommend testling, which executes tests according to your specification in all common browsers -- the code is running in actual browsers against your service.

我还可以推荐testling,它在所有常见浏览器中根据您的规范执行测试 - 代码在实际浏览器中针对您的服务运行。

Here's a simple example with jsdom:

这是jsdom的一个简单示例:

var jsdom = require("jsdom");

jsdom.env(url, ["http://code.jquery.com/jquery.min.js"], function(err, window) {
    // jQuery is at window.$
});

Instead of url above, you could have an HTML document, or fragment.

您可以拥有HTML文档或片段,而不是上面的URL。

You can also load a page and fetch any external resources, instead of providing jQuery etc directly to jsdom:

您还可以加载页面并获取任何外部资源,而不是直接向jsdom提供jQuery等:

var jsdom = require("jsdom").jsdom,
    doc = jsdom(markup),
    window = doc.createWindow();

// Do your stuff on window, jsdom will have fetched all the scripts referenced in the markup

Again, zombie.js uses jsdom internally and it might be a better starting point.

同样,zombie.js在内部使用jsdom,它可能是一个更好的起点。

#2


2  

There are two options for this

有两种选择

  1. Your testing browser code. Run it in the browser. Emulating the browser doesn't proof your code works, at all.
  2. 您的测试浏览器代码。在浏览器中运行它。模拟浏览器并不能证明您的代码是有效的。

  3. Use a tool like phantom / zombie
  4. 使用像幻影/僵尸这样的工具

Of course there are alternatives to this, you can extract any non-browser related code, write a unit test suite for them and run it in node. It's just JavaScript.

当然还有其他选择,您可以提取任何非浏览器相关代码,为它们编写单元测试套件并在节点中运行它。这只是JavaScript。

You can also use managed services like testling to run your browser tests for you

您还可以使用像testling这样的托管服务来为您运行浏览器测试