Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unittesting in TypeScript [closed]

I know, TypeScript is one day old. But I`m curious if is here somekind unit test framework or way how to write and run unit tests for TypeScript?

TypeScript can be compiled to JavaScript and I can write test for that JavaScript, but it`s not what I want.

like image 421
MicTech Avatar asked Oct 02 '12 18:10

MicTech


People also ask

How do I run a unit test in TypeScript?

For TypeScript, unit tests are run against the generated JavaScript code. In most TypeScript scenarios, you can debug a unit test by setting a breakpoint in TypeScript code, right-clicking a test in Test Explorer, and choosing Debug.

Should unit tests be written in TypeScript?

Another argument in favor of writing your unit tests in TypeScript is that unit tests serve as documentation. If you have unit tests code that exercises production code with the same type system, developers are more likely to understand the mechanisms of it all when, for example, attempting a refactor.

Where do unit tests go TypeScript?

We would follow the conventions: Place Source JS/TS files in src folder and tests typescript files in tests folder.

Does jest work with TypeScript?

Jest supports TypeScript, via Babel. First, make sure you followed the instructions on using Babel above. Next, install the @babel/preset-typescript : npm.


2 Answers

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.

like image 103
mohamed hegazy Avatar answered Sep 30 '22 20:09

mohamed hegazy


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).

like image 25
Fenton Avatar answered Sep 30 '22 20:09

Fenton