Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I unit test my JavaScript?

I'm curious to if it would be valuable, I'd like to start using QUnit, but I really don't know where to get started. Actually I'm not going to lie, I'm new to testing in general, not just with JS.

I'm hoping to get some tips to how I would start using unit testing with an app that already has a large amount of JavaScript (ok so about 500 lines, not huge, be enough to make me wonder if I have regression that goes unnoticed). How would you recommend getting started and Where would I write my tests?

(for example its rails app, where is a logical place to have my JS tests, it would be cool if they could go in the /test directory but it's outside the public directory and thus not possible... err is it?)

like image 526
JP Silvashy Avatar asked Jan 05 '10 23:01

JP Silvashy


People also ask

Can we write unit test for JavaScript?

JavaScript Unit Testing is a method where JavaScript test code is written for a web page or web application module. It is then combined with HTML as an inline event handler and executed in the browser to test if all functionalities are working as desired. These unit tests are then organized in the test suite.

Is JavaScript good for testing?

The JavaScript testing framework is a dynamic framework based on JS, which is well known for its ease of use in front-end and back-end development. These transitions over time also result in the need for excellent testing tools.

Is unit testing really necessary?

One of the benefits of unit tests is that they isolate a function, class or method and only test that piece of code. Higher quality individual components create overall system resiliency. Thus, the result is reliable code. Unit tests also change the nature of the debugging process.


2 Answers

Well, start with JsUnit. It sounds like you're more curious about unit testing in general, though.

The things you get from unit testing (if they're done right) are:

  • The ability to detect regressions in your code, as you mentioned
  • Less-painful integration, since each piece of your code is already tested by itself
  • A clear picture of how your code is expected (and not expected) to be used

Unit tests should basically touch any public method in your code. Sometimes you may have reason to test private methods, and I'm sure you can decide when that may be. The goal is simple:

  • Test that the method does the right thing with the right input
  • Test that the method does the right thing with the wrong input.

In many ways, your tests should define the functionality of your methods.

Sometimes when people write their unit tests, they intentionally "stub out" any integrated code (i.e., method calls that return other data from a database, file, or business logic) and make them return static data instead. This helps you to feel more confident that you're only testing the code present in the logic you're testing.

You may want to read on for more information about good and bad unit testing practices.

Edit: I don't know much about doing this in Ruby on Rails, but you might consider having a look at what some other people are doing. Ultimately, the tools available to you and the structure of your tests is going to depend on your framework and language.

like image 110
Ed Altorfer Avatar answered Oct 13 '22 04:10

Ed Altorfer


I found unit testing with javascript to be very helpful. Unit testing will compensate for the lack of type safety in the language. It also allows you to quickly verify your code running in different browsers.

For my tests I use QUnit as the test runner, and JSMock for mocking. I use firebug to debug them.

There are fewer educational resources out there for learning testing in Javascript then say C# or Java. Things are tested differently because its a dynamic language... It might be better to start testing in C# or Java.

I did not really become effective at writing unit tests til I read the material at www.xunitpatterns.com. So if you're just starting I would say buy the book and read it.

like image 40
Frank Schwieterman Avatar answered Oct 13 '22 05:10

Frank Schwieterman