I asked this question on the stackoverflow site: https://stackoverflow.com/questions/27469850/has-client-side-javascript-unit-testing-gotten-lost ...but I think it's a little to open ended. So instead...

Does it make sense to unit test client-side Javascript outside of the browser environment?

A few notes to inform readers: 1. I am not saying remove the browser from all testing procedures, just unit testing specifically.

  1. I know about using Node testing frameworks with Jsdom and am not looking for a framework suggestion.

I want to know what people think about unit testing outside of the browser. My main concerns for unit testing using a browser are:

  1. It's slower. This leads to longer feedback loops, devs will run the tests less often and discourages the use of tdd.

  2. Incorporating the DOM is a part of integration testing, right?

  3. Setting up a test environment in a browser can be a pain in the rear. E.g. loading modules and tests, keeping the dom clean, worrying about side effects like XHR, localStorage, cookies etc.

  4. It confuses what we are actually testing at the unit layer. E.g. Am I testing this view renders the elements I'm expecting or am I testing this view renders when I'm expecting it to?

Testing in the browser is important but isn't that the responsibility of integration/functional tests? It makes more sense to me to run unit tests outside of the browser, mock/stub all side effects such as the DOM, XHR etc.

To repeat my question: Does it make sense to unit test client-side Javascript outside of the browser environment?

Feedback is appreciated, just looking for some clarity, thanks!

1 Answer 1


Yes, it does, and even if we (our company) don't do it currently, we are working towards it as a goal.

One way (to gain speed) is use PhantomJS - headless browser (it executes all commands but does not paint screen unless you want a snapshot). This might, or might not, count as "browser environment".

Another way is to use advanced JS framework like Angular, and use Karma to unit-test components.

  • But would unit tests be more effective by removing the browser completely? Testing the logic of the views and logics without the environment. I've not entirely thought about how this would work yet. Dec 14, 2014 at 22:15

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.