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?)
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.
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.
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.
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:
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With