Unit testing is an essential part of software development. It helps to ensure that individual units or components of a software system are working as expected. In this post, we will discuss how to write unit test cases using Jest framework for code written in TypeScript.
For more blogs, Visit https://infinity-creator.blogspot.com/
Jest is a popular testing framework that is widely used by JavaScript and TypeScript developers. It provides an easy-to-use interface for writing test cases and comes with built-in assertions, mocks, and spies. Jest is also known for its speed and parallelism, which makes it an ideal choice for large-scale projects.
Before we dive into writing test cases, let’s first set up our project.
Setting up a TypeScript project with Jest.
To use Jest with TypeScript, we need to install the following packages:
- jest
- ts-jest
- @types/jest
We can install them using npm as follows:
npm install --save-dev jest ts-jest @types/jest
Once we have installed the required packages, we need to configure Jest to work with TypeScript. We can do this by creating a ‘jest.config.js’ file in the root of our project and adding the following code:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
};
Let’s go through each of these options in detail:
- preset: This option specifies the name of the preset to use. In our case, we are using the ts-jest preset, which enables Jest to work with TypeScript code.
- testEnvironment: This option specifies the test environment to use. In our case, we are using the node environment, which allows us to run our test cases in a Node.js environment.
- testMatch: This option specifies the file patterns that Jest should look for when running test cases. In our case, we are using the pattern ‘**/*.test.ts’, which tells Jest to look for files that end with .test.ts
With this configuration in place, we are now ready to write our first test case.
Writing a test case
Let’s say we have a TypeScript file math.ts that contains a simple function to add two numbers:
export function add(a: number, b: number): number {
return a + b;
}
To test this function, we can create a new file math.test.ts in the same directory and add the following code:
import { add } from './math';
test('adds two numbers', () => {
expect(add(1, 2)).toBe(3);
});
In this test case, we are importing the ‘add’ function from the math.ts file and using the ‘test’ function provided by Jest to define our test case. The first argument to ‘test’ is a description of the test case, and the second argument is a function that contains the actual test logic.
In our test case, we are calling the ‘add’ function with the arguments ‘1’ and ‘2’ and using the expect function provided by Jest to make an assertion. We ‘expect’ the result of the ‘add’ function to be ‘3’, so we use the ‘toBe’ matcher to check that the actual result is equal to the expected result.
Running the test case
To run the test case, we can use the following command:
npm test
This will run all the test cases that match the pattern **/*.test.ts in our project directory. If everything is set up correctly, we should see the following output:
PASS ./math.test.ts
✓ adds two numbers (3 ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
This indicates that our test case has passed successfully.
Conclusion
In this post, we discussed how to write unit test cases using Jest framework for TypeScript code. We set up Jest to work with TypeScript, wrote a simple test case, and ran the test case to verify that it passed. Writing unit test cases is an important aspect of software development, and Jest makes it easy to write and run tests for TypeScript code.
Let me know if you have any queries, Also if you want to know how to mock the objects.
References: