Open anu1667 opened 1 year ago
References - https://alemser.medium.com/integration-tests-an-approach-for-the-rest-api-ae86e47b7b43 https://circleci.com/blog/api-testing-with-jest/
How to run a RESTApi tests via Jest -
Problem faced running both express api and test in file Describe function running on node
Fixes Run the files separately Node doesn't support Describe
Tip Use try and catch methods to find errors for line -to -line checks
package.json
- express, supertestpackage-lock.json
to the repository. We can use npm ci
to install packages.
More explanation can be found here - https://stackoverflow.com/questions/52499617/what-is-the-difference-between-npm-install-and-npm-ci.ananditabendigeri@Ananditas-MacBook-Pro ~/init_exercises/EX5 main ± npm test
> package@1.0.0 test
> jest
console.log
Server is running on http://localhost:8080
at Server.log (app.js:24:13)
PASS ./api.test.js
GET /cal
✓ should return 0 (21 ms)
POST /cal
✓ should return the result of multiplication (7 ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 0.343 s
Ran all test suites.
Jest did not exit one second after the test run has completed.
'This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.
^C
At the end of jest test I receive this warning - This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with
--detectOpenHandlesto troubleshoot this issue.
Reason - The server doesn't close/stop listening after end of tests Solution - Try to start express server in test file itself and close after test. Something like this
beforeAll(() => {
//server start
});
afterAll(() => {
//server close
});
Simple REST Server Integration test
Add test code to the previous code.
Use
jest
moduleExecute the test after running the web api server in the terminal
Test’s requirement:
Regarding web server when doing
GET
, confirming thatanswer:0
is returned.Regarding web server when doing
POST
, confirming that the result of the multiplication is returned.