Javascript Unit Test - Unit Testing 101: Are You Testing Your JavaScript? - Test Driven Development: Top-Down or Bottom-Up? - Automating JavaScript Testing with QUnit

Mocks Aren't Stubs
Dependency Injection

To test a piece of code, you must be willing to separate the code from its surrounding, and also from things it depends on.

If a method is going to do some UI operation (or some other complicated operation), it is not useful to check the result of that operation. It is more important to see if your code asked for that operation to be performed. If we approach writing the test code from that point of view, it becomes easier to test the code. We can also entirely remove the UI from testing by using Mock objects. JavaScript is a dynamic language that makes it easy to mock.

var setFocusCalled = false;
function dummySetFocus() { setFocusCalled = true; }

var originalSetFocus = setFocus;
setFocus = dummySetFocus;
setFocus = originalSetFocus

YUI Test
JsSpec #2


We can stub out the server, and we can write tests to test our javascript code as long as our code does not cause the browser to go to another page. If our code cause the browser to go to another page, can we some stub out that page too?

Selenium limitation seems to be that it rely on when the onload handler to load itself. It seems that selenium should be a browser plugin for IE instead of HTA. If we use Selenium IDE testrunner mode, can we somehow cause Selenium IDE to always load selenium core ahead of other script?

A JavaScript function that involves both logic and DOM manipulation and event should be separated into 2 parts, one contains logic, one contains DOM manipulation and event. The part that contains logic can be tested easily. The part that contains DOM manipulation and event can also be tested using mocks, and stubs.


Test Driven Development: Top-Down or Bottom-Up?
Unit Testing 101: Are You Testing Your JavaScript?
JavaScript Test Driven Development with JsUnit and JSMock

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License