Javascript Programming Example: Add Unit Tests for the Version String Comparison
We have shown how to implement a version string comparison function with Javascript. As you may have noticed, we have to deal with some cases that can be easily overlooked. It is important that we add unit tests to this function. Whenever we modify the function, we can run these tests quickly to check if we break anything by mistake.
First, we need to extract the function and put it into a separate Javascript file version_compare.js.
Then we can use any Javascript unit test library to test this function. For example, we can use QUnit which is used for testing jQuery itself too. We can just create an HTML file with some QUnit-specific element IDs and add our tests to a function upon DOM ready event.
You can see the source file for running unit tests for the version comparison function.