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.
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.
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.
We would follow the conventions: Place Source JS/TS files in src folder and tests typescript files in tests folder.
Jest supports TypeScript, via Babel. First, make sure you followed the instructions on using Babel above. Next, install the @babel/preset-typescript : npm.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With