Unittesting in TypeScript [closed] Unittesting in TypeScript [closed] typescript typescript

Unittesting in TypeScript [closed]


TypeScript is not a runtime language. To execute your TypeScript code you first need to compile it to JavaScript; same applies to testing it.Your tests can be in TypeScript as well, compile both into JavaScript and use your favorite test framework to execute the tests.


You can write your unit tests in TypeScript or JavaScript, using any of the existing JavaScript unit testing frameworks.

Very soon, I imagine the existing frameworks will get TypeScript ambient definition files(update - they have: http://definitelytyped.org/ ), which will make them statically typed as far as TypeScript is concerned. In the meantime, you may need to read up on Ambient Declarations and add a few of your own at the start of your tests.

Alternatively, you can use tsUnit TypeScript Unit Testing Framework, which is a unit testing framework written in TypeScript - so it plays nice with TypeScript (and can be used in JavaScript too).


Seems there is another test runner/framework called Intern. https://theintern.github.io/

Here is an article explaining how to use it combined with TypeScript: https://www.sitepen.com/blog/2015/03/24/testing-typescript-with-intern/

Looks pretty promising when you are using TypeScript and you are looking for a unit-testing setup that supports source maps.

Example test:

import registerSuite = require('intern!object');import assert = require('intern/chai!assert');// Assume that we now have a version of our model in TypeScript:import SimpleTodoModel = require('todo/model/SimpleTodoModel');registerSuite({    name: 'SimpleTodoModel',    // Assume we have a promises interface defined    'default data'() {        var emptyModel = new SimpleTodoModel(),            id:string = emptyModel.get('id'),            length:number = emptyModel.get('todos').length,            incomplete:number = emptyModel.get('incomplete'),            complete:number = emptyModel.get('complete');        assert.strictEqual(id, 'todos-dojo',                    'Id should default to "todos-dojo"');        assert.strictEqual(length, 0,                    'Todos array should default to an empty array.');        assert.strictEqual(incomplete, 0,                    'Incomplete count should default to 0.');        assert.strictEqual(complete, 0,                    'Incomplete count should default to 0.');    }});