I'm a JS beginner. My question is do we always need to use a browser to test JS code? Is there a command prompt compiler/interpreter avilable ?
5 Answers
Rhino and Spidermonkey provide stand alone interpreters, but you'll need to find a DOM implementation if you want to test DOM manipulation, and you'll need to test in any browsers you are targeting anyway (not all JS engines are created equal).
-
2"not all JS engines are created equal" - this is a very important point. If you expect your JavaScript to run in a browser, you'll want to test it in a browser. Sep 28, 2010 at 12:38
Not only use a browser to test, but use all browsers to test -- as a web developer, it is your responsibility to ensure that the code you write will run for all users who visit your site (or as many as feasibly possible). So if your site is available to the general public, you really should be testing in all the browsers that will be used by the general public.
That said, I (and probably most devs) do tend to stick to one browser for main dev work, and only switch to the others for testing, every now and then.
As for command-line Javascript, my favourite way of doing that is using Firebug, which is a plug-in for the Firefox browser aimed at helping developers debug their Javascript code. It has a console mode, which you can use to execute arbitrary javascript code, which can be very handy for debugging (it has many other fabulous features; if you haven't tried it already, I seriously recommend it).
JavaScript on its own is nothing more than just a language. It is useless without an Object Model. You can create loops, functions, etc etc, but you have to work with what you have in the particular JavaScript environment. For instance, if you use Windows Script Host- you get only console input and output. If you need web environment - document, window, body, getElementById
method - you need an environment which provides all these objects. And web browsers do it the best.
If you use JavaScript for client side web then you can have JS tests automated with Selenium. Depending on your platform (PHP, Ruby, Java) and framework (e.g. Ruby on Rails...) you shouldn't have problems to find proper integration binding.
Selenium starts browser in the background and performs all tests with it. The gain here is that your tests are unattended and against real browser you chose: Firefox, IE, Opera, not some limited interpreter.
You can also run Selenium headlessly - without opening browser window.
If you don't want to use selenium you can try other alternatives.
-
This still tests the JavaScript in a browser, though (albeit automatically). Sep 28, 2010 at 13:16
-
@Ludvico ok, but OP is JS beginner and it seems that using browser to test I suppose by clicking is hassle for him/her. And how are you going to properly test JS intended for browser, but without using browser? You can validate syntax, do some mocks, but what with DOM, events etc?– gertasSep 28, 2010 at 13:23
Yes you should. Since the standard is not followed by everyone each browser may behave different depending on your code. So it is better to test it on the browsers.