Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test driven development for a JavaScript library

Tags:

javascript

tdd

At the moment I'm working on a JS library for a webservice, you can compare it with Twitter Anywhere. Now i want to make it more test-driven. It's not easy to test because it has to work on every site that wants to make use of it, and of course with every browser.

How can i test the library efficiently?

All the API requests and responses are in JSON, is there a good way to test these calls?

I know about Cucumber and js-test-driver.

Greetings, Chielus

like image 313
Chielus Avatar asked Mar 05 '11 12:03

Chielus


2 Answers

Javascript language is dynamic by nature, so it is really test-driven friendly. I've recently got a little experience with javascript testing. I've rewrote major javascript components using TDD and got clear desing and more compact code!

  1. unit test framework of choice is qUnit. It is very easy to start with testing.
  2. functional test framework of choise is funcunit.

I did a blog post of testing REST api with FuncUnit here.

If you need some examples of tests and implementation, you can check my github repository.

Don't ask questions, just start testing :)

like image 181
Alexander Beletsky Avatar answered Nov 14 '22 07:11

Alexander Beletsky


If you know about jsTestDriver I think you've already found a good solution?

You can use it to automatically launch your tests in multiple browsers and return success or failure.

This sets it apart from other tools that use headless browsers, as with jsTestDriver you're running your tests in real browsers, which seems to meet your requirements.

jsTestDriver comes with its own limited assertion framework but you can plug others into it including QUnit, YUI and Jasmine.

You said above in relation to Jasmine, "I don't think i can do BDD, because it's a library that has to work with all kinds of sites.". I'm not sure what you mean by this?

Jasmine provides all the assertions to let you do the same tests as QUnit. It also lets you 'spy' on Ajax callbacks, intercept the JSON to examine or even alter it, then pass it on to your default callback. With this you could check the JSON response then check again when your UI has reacted to it in the right way.

like image 20
monkeyboy Avatar answered Nov 14 '22 07:11

monkeyboy