Why Jest for microservices integration testing or Api automated testing
World is rapidly moving away from Monolith to Micro-services. Many startups or even huge companies are developing their new system in Micro-services. As there is demand for Micro-services developers increase, so there is increase in demand for Api automation testers.
As javascript is becoming more and more popular nowadays, there is more demand for api automation testers who knows javascript/typescript. Check out how javascript popularity is increasing:
link: https://madnight.github.io/githut/#/pull_requests/2021/1
There are many popular testing libraries available for javascript:
- Jest
- Mocha
- Jasmine
- Ava
Then Why jest:
Based on my experience in api testing using different javascript testing libraries, I think below are the key advantages jest possess and others don’t
- Popular
- Out of the box setup
- Snapshot testing
- Intelligent parallelisation
- Flexible Assertion
Let’s see in detail
Popular
Key decision for choosing a library is to see how popular among the community. Here is the data of how popular jest is:
go check for yourself: https://www.npmtrends.com/jest-vs-mocha-vs-jasmine-vs-ava
Out of the box setup
Configuring Jest is a piece of cake. It is very straight forward and intuitive , try yourself: https://jestjs.io/docs/getting-started
Also Jest bundles all the popular packages required for test automation:
- For Assertion — Expect. If you use mocha you need to install chai separately.
- Env file configuration: you can use this to easily setup the files before running the tests.
- You can easily mock any request lib using jest mock. In case of other runners you need to install the mocking lib separately.
Snapshot testing
No other framework in javascript have this feature of snapshot testing.
For Api testing we need to assert lot of json objects such as header object, body object of the response. When the there are 100 of different request and response pairs. We need keep updating the expectations json file whenever there is a change in response schema. This can be avoided using jest.
Snapshots are automatically created just by:
expect(data).toMatchSnapshot();
And to update a snapshot there are easy way to update by CLI commands whereas in other test frameworks you need to find the relevant json file and update it.
Parallelisation
Jest runs testcases in parallel by default and it runs smartly. So you dont need to mention anything in the configuration to mention how many instance to run in parallel. Only thing to make sure is that tests are not sharing any state
Assertion
Jest has this awesome assertion lib in-build and it provides various advantages than others.
For example: expect.extend(matchers).
let’s say that you’re testing a number utility library and you’re frequently asserting that numbers appear within particular ranges of other numbers. You could abstract that into a toBeWithinRange
matcher. Link.
Documentation
Last but not least the documentation, It has amazing documentation and api compared all other testing libraries in javascript which is really important for any development needs.
Now we have all reasons to start writing our integration tests in jest. 🚀
So what’s next? here comes the series for Jest Automation framework setup:
1. https://vinodmuralidharan.medium.com/api-test-automation-framework-jest-understanding-ab4fbb471c2c
2. https://vinodmuralidharan.medium.com/api-test-automation-framework-jest-setup-12226b41e267