Topic: Test-Driven Development & Javascript

(I think this is the right forum for this post, if not, feel free to move it.)

For those of you who practice test-driven development (and those of you who want to) with your Javascript code, you may find the following website useful:

It hosts a Web 2.0 game. The point of the game is to test-drive the development of a Javascript function, by incrementally passing a series of 13 failing testcases.

In test-driven development, you write the test first, then write enough code to pass it, and finally refactor, making sure that no old tests break at any step of the development.

TDD, as it's called, is widely practiced by Rails folk but not so much in Javascript code. However, it can be done. Give it a try!

Re: Test-Driven Development & Javascript

I think there's a problem with your function:

It relies on f(d); to change the value of d so TDD.assertEquals(d, array()) will pass. However in javascript, you cannot pass variables by reference so d can never be changed.

Re: Test-Driven Development & Javascript

Hi Crux,

Well, arrays are passed by value; however, the value that is passed is actually a reference to an array. So you can change the values of an array that is passed in as a parameter. But you cannot assign it to a new array.

This is not valid:

invalid = function(myArray) {
    myArray = new Array();
var a = new Array();

When the function executes, it won't have succeeded in changing the array: 'a' will still point to the same array, and won't contain anything.

Hope that helps!