Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jest Testing #32

Open
samlipari opened this issue Mar 26, 2022 · 1 comment
Open

Jest Testing #32

samlipari opened this issue Mar 26, 2022 · 1 comment

Comments

@samlipari
Copy link
Contributor

No description provided.

@MFatigati
Copy link
Contributor

MFatigati commented Apr 19, 2022

Key things to do for writing tests:

  1. Organize test files by name of function called directly by API route (since putting the API route in the file/folder names doesn't work) .
  2. In the describe string, inform about what API route these tests relate to.
  3. In the test string, information which specific function is being tested.
    • either the function called by the API route which sends data and catches errors (e.g., listServices)
    • or the helper function invoked by the aforementioned function, which takes care of the "business logic" (e.g., getServicesList).
  4. Three tests seem sensible for each route/route-related function:
    • Test whether the "business logic" returns what you want by hard coding inputs and outputs, and mocking axios calls.
    • Test whether the function called directly by the API calls the proper "business logic" function by "spying" on the module (important that the "business logic" function is imported all around on a module, not by itself).
    • Test whether the live function returns the expected status code and content type, under normal circumstances, using supertest.

Note that for supertest, we needed to separate out the module app from index.js. The start script still calls index.js, but we needed to be able to export the app into each test using supertest, without "listening" on port 3000.

To get the hard-coded input data, I would do the following: run the routes in Postman, returning the FULL response from the Docker/Prometheus API, before transforming it in whatever way the function demands. Use this as the "mock resolved value" for the relevant axios call. To get the hard-coded output data, I would just use the result from Postman, based on the aforementioned input. This only works if you already are confident your functions are doing what they are supposed to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants