What is TypeScript?
TypeScript (TS) is a programming language that adds type safety to JavaScript (JS). It is basically JavaScript + types, e.g.
However, TypeScript never gets executed. Instead, TypeScript files with .ts extensions gets transpiled to JavaScript .js files, which in turn can be executed on Node.js.
This article assumes a beginner to intermediate level of expertise in JavaScript and TypeScript.
In this article, I will contrast and compare two different approaches to writing your unit tests:
- In a JavaScript file, unit testing against already transpiled code from TypeScript, or
- In a TypeScript file that is type safe against the system under test.
I am a Newbie Back-End engineer
A quick disclaimer; I only recently completed the Back-End Engineer learning path from Codecademy. The learning track uses JavaScript as programming language, and Node.js together with Express.js as web server framework.
The Back-End engineering track from Codecademy is pretty extensive, and I would recommend it to anyone who is curious about back-end or who, like me, wants to become a Full-Stack engineer.
In my case, Full-Stack means an Express.js app hosted on cloud services with elastic resources, along with an Android application to match, while for you it could mean Ruby, Ruby on Rails, and React Native, etc.
However, as good a career path from Codecademy as it is, it is missing something that I believe to be of paramount importance; lessons on TypeScript.
Luckily, Codecademy also offers a separate course track conveniently caProject Structure in TypeScriptlled Learning TypeScript.
Project Structure in TypeScript
The project structure when using TypeScript is very similar to that of a JavaScript project that runs on Node.js.
In our case, TS files are located in ./src and ./test, while their transpiled counterparts are in ./built/src and ./built/test respectively.
Writing unit tests
As mentioned previously, there are two workflows to writing and executing unit tests when developing in TypeScript:
- Write the unit tests in TypeScript, run tsc to compile both the program’s files and its unit tests, and then run the unit tests.
- Compile the program’s files into JavaScript first, and then write unit tests in JavaScript against the JavaScript code.
Should you write your unit tests in TypeScript?
There are many advantages to writing your unit tests in TypeScript. For one, you can develop your application using the Test-Driven Development (TDD) paradigm. Statistics for successful software development projects are in favor of TDD.
By developing your unit tests first, you build the type safety scaffolding around which you can build a robust application right from the beginning.
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.
Moreover, having unit tests written in TS will ensure homogeneity of an idiomatic code base. It might not seem like a big deal, but homogeneity increases the chances of finding bugs in production code, hence reducing costly maintenance of technical dept.
How about writing unit tests in JavaScript instead?
The biggest advantage to writing your unit tests in JavaScript is the iteration speed of the red-green-refactor cycle. Although TDD was mentioned as an advantage in favor of TypeScript, it is a double-edged sword that cuts both ways.
The time taken to fix a red unit test to green, because of transpiling overhead, is often a source of frustration when the test is written in TypeScript.
Moreover, a primordial requirement when writing unit tests, or debugging in general, is the ability to place breakpoints.
I played around with both VSCode and WebStorm to get a feel of whether it could be done, and it seems that only WebStorm allows you to “run” unit tests in TS.
In conclusion, I feel that whether you write your unit tests in JavaScript or in TypeScript heavily depends on the IDE you are using for your development needs.
I personally started developing on WebStorm, and I will continue to do so for the foreseeable future. I write my unit tests in TypeScript.
With WebStorm I am able to to run unit tests in a targeted, individual, way within a few clicks of the mouse. This feature is the proverbial nail in the coffin of JavaScript’s unit tests.