Eshwari247 / init_exercises

0 stars 0 forks source link

Simple REST Server Integration test #7

Open anu1667 opened 1 year ago

anu1667 commented 1 year ago

Simple REST Server Integration test

Add test code to the previous code.

Test’s requirement:

Regarding web server when doing GET, confirming that answer:0 is returned.

Regarding web server when doing POST, confirming that the result of the multiplication is returned.

Eshwari247 commented 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/

Eshwari247 commented 1 year ago

How to run a RESTApi tests via Jest -

Eshwari247 commented 1 year ago
anu1667 commented 1 year ago

Test

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
});