5

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.

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

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

1
  • This is a very underdeveloped part of software. Took me forever to find a way to even run my JS tests with my other tests in Visual Studio. Commented Mar 1, 2012 at 14:00

2 Answers 2

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.

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.

Here's a simple example with 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.

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

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What's your consumption of testling minutes like? I'm always worried I would run my tests "too much" and consume "too many" minutes
Raynos: I have actually just tried it out. I'm in the verge of launching and as soon as business takes off, I'm pretty sure testling will be well worth the cost, since I hate manual browser testing. :-)
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. Use a tool like phantom / zombie

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.