Oops! Something went wrong while submitting the form.
We use cookies to improve your browsing experience on our website, to show you personalised content and to analize our website traffic. By browsing our website, you consent to our use of cookies. Read privacy policy.
Before starting off, below listed are the reasons why API testing should be encouraged:
Identifies bugs before it goes to UI
Effective testing at a lower level over high-level broad-stack testing
Reduces future efforts to fix defects
Time-saving
Well, QA practices are becoming more automation-centric with evolving requirements, but identifying the appropriate approach is the primary and the most essential step. This implies choosing a framework or a tool to develop a test setup which should be:
Scalable
Modular
Maintainable
Able to provide maximum test coverage
Extensible
Able to generate test reports
Easy to integrate with source control tool and CI pipeline
To attain the goal, why not develop your own asset rather than relying on the ready-made tools like Postman, JMeter, or any? Let’s have a look at why you should choose ‘writing your own code’ over depending on the API testing tools available in the market:
Customizable
Saves you from the trap of limitations of a ready-made tool
Freedom to add configurations and libraries as required and not really depend on the specific supported plugins of the tool
No limit on the usage and no question of cost
Let’s take Postman for example. If we are going with Newman (CLI of Postman), there are several efforts that are likely to evolve with growing or changing requirements. Adding a new test requires editing in Postman, saving it in the collection, exporting it again and running the entire collection.json through Newman. Isn’t it tedious to repeat the same process every time?
We can overcome such annoyance and meet our purpose using a self-built Jest framework using SuperTest. Come on, let’s dive in!
Creating a node project jest-supertest in our local and doing npm init. Into the workspace, we will install Jest, jest-stare for generating custom test reports, jest-serial-runner to disable parallel execution (since our tests might be dependent) and save these as dependencies.
coverageDirectory: creates a custom directory for coverage reports
coverageReporters: format of reports generated
coverageThreshold: minimum and maximum threshold enforcements for coverage results
Testing endpoints with SuperTest
SuperTest is a node library, superagent driven, to extensively test Restful web services. It hits the HTTP server to send requests (GET, POST, PATCH, PUT, DELETE ) and fetch responses.
Let’s write commonTests.js which will be required by every test file. This hits the app through SuperTest, logs in (if required) and saves authorization token. The aliases are exported from here to be used in all the tests.
[Note: commonTests.js, be created or not, will vary as per the test requirements]
Moving forward to writing our tests on POST, GET, PUT and DELETE requests for the basic understanding of the setup. For that, we are creating two test files to also see and understand if the sequencer works.
Once complete, the test results will be immediately visible on the terminal.
Test results HTML report is also generated as index.html under jest-stare/
And test coverage details are created under coverage/my_reports/ in the workspace.
Similarly, other HTTP methods can also be tested, like OPTIONS - supertest.options() which allows dealing with CORS, PATCH - supertest.patch(), HEAD - supertest.head() and many more.
Wasn’t it a convenient and successful journey?
Conclusion
So, wrapping it up with a note that API testing needs attention, and as a QA, let’s abide by the concept of a testing pyramid which is nothing but the mindset of a tester and how to combat issues at a lower level and avoid chaos at upper levels, i.e. UI.
I hope you had a good read. Kindly spread the word. Happy coding!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Building A Scalable API Testing Framework With Jest And SuperTest
Focus on API testing
Before starting off, below listed are the reasons why API testing should be encouraged:
Identifies bugs before it goes to UI
Effective testing at a lower level over high-level broad-stack testing
Reduces future efforts to fix defects
Time-saving
Well, QA practices are becoming more automation-centric with evolving requirements, but identifying the appropriate approach is the primary and the most essential step. This implies choosing a framework or a tool to develop a test setup which should be:
Scalable
Modular
Maintainable
Able to provide maximum test coverage
Extensible
Able to generate test reports
Easy to integrate with source control tool and CI pipeline
To attain the goal, why not develop your own asset rather than relying on the ready-made tools like Postman, JMeter, or any? Let’s have a look at why you should choose ‘writing your own code’ over depending on the API testing tools available in the market:
Customizable
Saves you from the trap of limitations of a ready-made tool
Freedom to add configurations and libraries as required and not really depend on the specific supported plugins of the tool
No limit on the usage and no question of cost
Let’s take Postman for example. If we are going with Newman (CLI of Postman), there are several efforts that are likely to evolve with growing or changing requirements. Adding a new test requires editing in Postman, saving it in the collection, exporting it again and running the entire collection.json through Newman. Isn’t it tedious to repeat the same process every time?
We can overcome such annoyance and meet our purpose using a self-built Jest framework using SuperTest. Come on, let’s dive in!
Creating a node project jest-supertest in our local and doing npm init. Into the workspace, we will install Jest, jest-stare for generating custom test reports, jest-serial-runner to disable parallel execution (since our tests might be dependent) and save these as dependencies.
coverageDirectory: creates a custom directory for coverage reports
coverageReporters: format of reports generated
coverageThreshold: minimum and maximum threshold enforcements for coverage results
Testing endpoints with SuperTest
SuperTest is a node library, superagent driven, to extensively test Restful web services. It hits the HTTP server to send requests (GET, POST, PATCH, PUT, DELETE ) and fetch responses.
Let’s write commonTests.js which will be required by every test file. This hits the app through SuperTest, logs in (if required) and saves authorization token. The aliases are exported from here to be used in all the tests.
[Note: commonTests.js, be created or not, will vary as per the test requirements]
Moving forward to writing our tests on POST, GET, PUT and DELETE requests for the basic understanding of the setup. For that, we are creating two test files to also see and understand if the sequencer works.
Once complete, the test results will be immediately visible on the terminal.
Test results HTML report is also generated as index.html under jest-stare/
And test coverage details are created under coverage/my_reports/ in the workspace.
Similarly, other HTTP methods can also be tested, like OPTIONS - supertest.options() which allows dealing with CORS, PATCH - supertest.patch(), HEAD - supertest.head() and many more.
Wasn’t it a convenient and successful journey?
Conclusion
So, wrapping it up with a note that API testing needs attention, and as a QA, let’s abide by the concept of a testing pyramid which is nothing but the mindset of a tester and how to combat issues at a lower level and avoid chaos at upper levels, i.e. UI.
I hope you had a good read. Kindly spread the word. Happy coding!
Velotio Technologies is an outsourced software product development partner for top technology startups and enterprises. We partner with companies to design, develop, and scale their products. Our work has been featured on TechCrunch, Product Hunt and more.
We have partnered with our customers to built 90+ transformational products in areas of edge computing, customer data platforms, exascale storage, cloud-native platforms, chatbots, clinical trials, healthcare and investment banking.
Since our founding in 2016, our team has completed more than 90 projects with 220+ employees across the following areas:
Building web/mobile applications
Architecting Cloud infrastructure and Data analytics platforms