Introduction to TypeScript Unit Testing
TypeScript Unit Testing is a testing technique that picks out the small prudent unit of code that functions. In 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 sure is working correctly? And every unit testing can be done separately and independently for the added unit, and 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, unit tests are run 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 need to put down 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 need to define a test script that is needful 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 be able to create a project by following a similar attitude.
So, begin with the project folder layout in which we can able to 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 has been inclined by Mockito for Java.
Then we need to configure the ‘tsconfig.json’ if we have a root ‘tsconfig’, and we need to 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 which enhances the performance.
We need to append two 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 need to run the tests, open the package.json, and append the following command in the script object.
"test": "nyc ./node_modules/.bin/_mocha 'test/**/*.ts'",
TypeScript Unit Testing Project
The CLI-build projects carried in Visual Studio 2022 can function along test explorer, and the jest is the testing framework that can be utilized for React and Vue projects so that we can route the default test which is given by every framework and also can able to interpret the extra tests, and we just need to ruin the ‘Run’ button in test explorer.
If we do not have the open explorer then we require to find out by choosing Test >Test Explorer which is available in the menu bar. Node.js is essential for managing unit testing. Mocha and Tape test libraries have also been assisted by the CLI-build projects.
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 is the problem.
- Unit testing can give feedback on the quality of the code.
- It can be considered as a protection over the regression.
- It is also considered a workable code document.
- Because of unit testing re-writing of 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', () => {
});
});
In the above example, we can able to write test cases after the ‘describe’ method.
Conclusion
In this article, we conclude that unit testing is the first level of testing in which small units of the code have been tested. We have also seen how to create typescript unit testing, setting up of it, projects, examples, and benefits. So this will help to understand the concept.
Recommended Articles
This is a guide to TypeScript Unit Testing. Here we discuss the introduction, and how to create TypeScript unit testing. project, benefits, and examples. You may also have a look at the following articles to learn more –