Jest
I used Jest
to add unit tests in my typescript project, which is straight-forward and I used to work with Jest
before, I felt it was handy.
What is Jest?
Jest is a JavaScript testing framework designed to ensure correctness of any JavaScript codebase.
Jest with Typescript
Prerequisite
npm i -D jest typescript
Step1
npm i -D ts-jest @types/jest
Step2
npx ts-jest config:init
Step3
Create {file_name}.test.ts
for unit testing and run the following command.
"test": "jest -c jest.config.ts --runInBand --",
"test:watch": "jest -c jest.config.ts --runInBand --watch --",
npm run test
command is to run all unit test files such as {file_name}.test.ts
and npm run test:watch
is to keep watching your test results when you fix your code.
Also, you can check the coverage based on your unit testing and it shows how many unit test covers your code, this is the script for checking coverage:
"coverage": "jest -c jest.config.ts --runInBand --coverage"
Learning Opportunity
When I started with my project, my priority was to implement functionalities and run the project properly, so I really didn't think about edge cases and fail cases. Always assume that my app is working properly. However, this time I found out I missed lots of error handling function by using try
and catch
, so I refactored my codes and improved the quality of my project. Also, I realized unit testing is much harder and more important than implementing the features. It requires more energy and effort to verify my project works properly. I'll be getting used to unit testing and next time I'll implement an integration test with Github actions
Top comments (0)