开发者

Should I unit test my JavaScript?

开发者 https://www.devze.com 2022-12-15 08:29 出处:网络
I\'m curious to if it would be valuable, I\'d like to start using QUnit, but I r开发者_StackOverfloweally don\'t know where to get started. Actually I\'m not going to lie, I\'m new to testing in gener

I'm curious to if it would be valuable, I'd like to start using QUnit, but I r开发者_StackOverfloweally 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?)


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.


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.


One of the best guides you can find on incorporating testing into old code is Working Effectively with Legacy Code. In your case, you don't have a large amount of code you need to worry yourself about. Just start putting it in where you can and think about how more easily you could structure your code to allow for tests in general.


Testing JavaScript directly is not trivial (because it needs an "outside" interpreter, which in production env is the browser). Also therefore it is difficult to get it included in your continous-integration environment.

So because JavaScript unit-testing is very high effort, I would tend to test stuff more coarse grained in integration-tests. For instance: canoo-webtest incorporates a java-script interpreter. You fake user-actions (e.g. clicking button) and the javascript gets triggered. So you test indirectly.

Still there is some UI-related javascript stuff (e.g. fade-effects) etc.. This needs to be tested manually.


With Rails, I'd recommend Blue Ridge. It's a packaging of ScrewUnit, some rake tasks, along with the ability to run tests out of browser (via Rhino). We have quite a few Javascript tests up and going. The tests are more similar to RSpec than other tools mentioned, so it's less of a shift in mindset... works great!

To get started, the best place to do so is just start poking around the web for other people who have been successful with this. There are examples over on github.

0

精彩评论

暂无评论...
验证码 换一张
取 消