Powered by Blogger.

TypeScript–Surprisingly Simple, Extraordinarily Powerful– Part 7


Unit Testing:

TypeScript is just an extension of JavaScript hence we can use any of the existing testing frameworks available today like Jasmine, qUnit, YUI Test to write unit test cases. In most of the scenarios, ambient declarations (*.d.ts) files will be available to assist writing test cases. But with some frameworks, we may find naming clashes for example qUnit has a function named module in the global scope, which clashes with the reserved word in TypeScript.

There is another testing framework specifically created for TypeScript i.e. tsUnit available at http://tsunit.codeplex.com/

tsUnit just contains 1 file “tsUnit.ts” which contains all the things needed for writing unit tests. The concept of Arrange, Act and Assert stand for tsUnit as well, wherein we first arrange the data to be used for testing, then act on that data by calling the required function which needs to be tested and finally assert to check whether the value returned from this method matches to what we were expecting.

The assert is achieved by tsUnit.TestContext which provides various methods for asserting:


  • are identical: As the name suggests test two arguments for both type and value. This means that a “string of 1” will not match a number of 1.test two arguments for both type and value. This means that a “string of 1” will not match a number of 1. To pass the areIdentical test, the values must be the same type and the same value for primitive types or the same instance of an object for a complex type.
  • are not identical: It is just a reverse of are identical
  • isTrue: It checks that a Boolean parameter is true.
  • is false: It checks that a Boolean parameter is false.
  • Throws: Throws takes a function as a parameter, in which we call the function to be tested. The function to be tested must raise an error when it runs. This can help us check whether the function is handling exceptions as expected.
  • Fail: This assertion forces the test to fail and is a useful writing test to make sure we don’t miss the test, it acts as a placeholder until the real implementation is written.

tsUnit also provides an option to fake the dependencies using the tsUnit. Fake which can provide the fake implementation of the dependency. This is very useful because we would not want to write test cases that would have a dependency on other classes as that would cause test cases to be very brittle.

To run a test using the tsUnit, it provides a “run()” function which is accessible through the tsUnit.Test(). Before calling the run function we would need to register the test cases with the tsUnit.Test() class by calling “addTestClass” as

var test = tsUnit.Test();

test.addTestClass(new <<name of the test class which has test functions>>;

then we can just call test.run() to execute all the test registered.

test.run() returns testResult which contains the array of “errors” class which in turn have properties like “testName”, “funcName” and “message”.

Next Up: Useful Links for learning TypeScript

No comments