Updated June 28, 2023
Introduction to TypeScript Unit Testing
TypeScript Unit Testing is a technique that picks out the small prudent unit of code that functions. We can say that if we have a function that can calculate the sum of two numbers which unit test can be done to confirm that it is working correctly? And every unit testing can be done separately and independently for the added unit. The unit test is the code that can implement the other code to prove that the code which is running is working correctly.
What is TypeScript Unit Testing?
Unit testing is one of the best steps of our codebase at a similar flow. To put down the test case is very unexciting, and it could be one more sort of sub-application that can be close to our central code; if it has been controlled accurately, then the unit test can reserve our life and brains when debugging the code. For TypeScript, developers run unit tests over the created JavaScript code; in most cases, we can debug the unit test by setting a breakpoint in the code.
How to Create TypeScript Unit Testing?
For creating the TypeScript unit testing, we need to follow some conventions:
- We must put the JS/TS file in the ‘src’ folder and the tests typescript file in the ‘test’ folder.
- Then we have to install an ‘npm’ package for TypeScript and then define a test script that is necessary to carry out the test cases.
- For executing the tests in Node, we have to define the ‘scripts’ for the test in ‘package.json’.
- For debugging the TypeScript test, the JSON should have to describe within the ‘VS code debug’ sector.
- And then, we need to append the configuration in VS code; npm commands are there, which can be used for debugging.
TypeScript Unit Testing Setting Up
Let us see the setup of an opinionated unit testing environment with typescript through which we can also create a project by following a similar attitude.
So, begin with the project folder layout in which we can arrange our tests by following the same internal structure of the ‘src’ folder; it also operates when we want to support the common attitude.
root
| node_modules
| src
| test
| package.json
| tsconfig.json
TypeScript projects require some dependencies:
"npm install --dev ts-node mocha @testdeck/mocha nyc chai ts-mockito"
What we will receive from it:
- ts-node: This has been used to run the ‘.ts’ files by compiling them promptly.
- Mocha: It has been utilized as a test runner.
- @testdeck/mocha: With the help of this, we can able to write the TypeScript classes like test suites.
- nyc: It can able to create the coverage report.
- chai: It can be utilized as an assumption library.
- ts-mockito: It is a mocking library that Mockito has inclined for Java.
Then we need to configure the ‘tsconfig.json’ if we have a root ‘tsconfig’ and create a ‘tsconfig.json’ for testing inside the ‘test’ folder.
| node_modules
| src
| test
| --- tsconfig.json
| package.json
| tsconfig.json
Then we need to register the file for loading the ‘tsconfig’ instrument on the ts-node, enhancing the performance.
We need to append files such as ‘./.mocharc.json’ and ‘./.nycrc.json’.
./.mocharc.json:
{
"require": "./register.js",
"reporter": "dot"
}
./.nycrc.json:
{
"extends": "@istanbuljs/nyc-config-typescript",
"include": [
"src/**/*.ts"
],
"exclude":[
"node_modules/"
],
"extension":[
".ts"
],
"reporter":[
"text-summary",
"html"
],
"report-dir": "./coverage"
}
At the final stage, we must run the tests, open the package.json, and append the following command to the script object.
"test": "nyc ./node_modules/.bin/_mocha 'test/**/*.ts'",
TypeScript Unit Testing Project
The CLI-build projects carried out in Visual Studio 2022 can function along with test explorer. The testing framework that can be utilized for React and Vue projects is Jest. You can use Jest to run the default test provided by each framework and interpret additional tests. We need to ruin the ‘Run’ button in test explorer.
If we do not have the open explorer, we must find out by choosing Test >Test Explorer, which is available in the menu bar. Node.js is essential for managing unit testing. The CLI-build projects have also assisted Mocha and Tape test libraries.
Benefits
- Unit testing is fast, and it gives rapid feedback to the developers.
- When it fails, then generally, we can able to find out where the problem is.
- Unit testing can give feedback on the quality of the code.
- One can consider it as a protection against regression.
- Many consider it a workable code document.
- Because of unit testing, re-writing the code is feasible.
Example of TypeScript Unit Testing
Let us see an example to set a ‘Calculator’ class that has ‘Add’, ‘Subtract’, ‘Multiply’, and ‘Divide’ methods, and then append a ‘describe’ function and provide a string value that can outline the set of tests, we can say that it is a good objective to append a ‘describe’ method for every test or group of test cases under a group and the code will be given below.
describe ('Calculations from Calculator class', () => {
describe (‘add two numbers', () => {
});
describe ('subtract two numbers', () => {
});
describe ('multiply two numbers', () => {
});
describe ('divide two numbers', () => {
});
});
We can write test cases after the ‘describe’ method in the above example.
Conclusion
In this article, we conclude that unit testing is the first testing level in which small code units have been tested. We have also seen how to create typescript unit testing, setting it up, projects, examples, and benefits. So this will help to understand the concept.
Recommended Articles
We hope that this EDUCBA information on “TypeScript Unit Testing” was beneficial to you. You can view EDUCBA’s recommended articles for more information.