When you are writing JavaScript unit tests, where do you put all the test code?

I see two potential approaches:

  1. Leave all your test code with your regular web project and explictly don't release the code to production.

  2. Maintain a separate project that runs all your tests.

I see pros and cons for both. I'm curious what more experienced JavaScript developers have ended up using.

  • 1
    I don't love TDD so I comment rather than answer (I do have some appreciation for automated testing of event-driven objects so it might not be a total waste). And of course, take what I say as a known curmudgeon with grain-of-salt, etc... JavaScript for the web has the following dependencies: !@#%$ the server does, !@#$ the browser does, !@#$ the user does, !@#$ Microsoft doesn't do, and quirky !@#$ everybody else does slightly differently from everybody else. Don't get fancy. Include or don't include a file with tests. Run it in every platform you care about before publishing new stuff. Done. – Erik Reppen Jul 3 '12 at 3:20

Unit Tests

For real JavaScript unit tests, usually I'd recommend that they can be executed by a command-line tool, or by a browser-based tool but where the unit-tests are still separate from the main code base.

Lately, the frameworks I've found to be the most practical to use where Jasmine and JsTestDriver, both in terms of API design and tooling (for instance, to integrate with a Maven build, which already enforces the separation of source code and test code in general). Some other automated and head-less tools offer some level of support of JavaScript as well (like HtmlUnit), but unfortunately it's usually the browser-based ones that provide the best results (but are also the hardest to integrate and maintain). So, usually, the test-code is part of the same project, but clearly separated and not part of the generated output.

Integration Tests

On top of that, obviously some JavaScript aspects rely heavily on the UI, and you may want to have more complete integration tests (using something like Seleniun, for instance). For these, it's not uncommon to have them in separate projects, especially as integration tests may rely on multiple modules.


We organize and run our unit tests separately :

  • Must be able to run tests from the commend-line
  • Tests should be relatively easy to write

Unit tests are the design specs of the module’s behavior, they are not an observation of everything the code does. Unit tests query how the module does its job, rather than what it does.

After some re-search we ended up with rhinounit - Ant Based Javascript Testing Framework. However, there might be some other new frameworks that i would be glad to hear about.

There is also a comparison of RhinoUnit vs JSUnit net that might be interesting to look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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