Skip to content

Backend Tests

Dinith edited this page May 24, 2020 · 5 revisions

Testing tools used:

  • jest

    • Allows easy testing of any JS code
    • Allows use of mocks
    • Allows use of spies
  • mockingoose

    • Allows mocking of Mongoose models
    • Can mock Mongoose method calls on the models
  • mongodb-memory-server

    • Allows mocking of a MongoDb server
    • Creates an in-memory database

Testing the vanilla JS code

Testing of the backend's methods was done, namely the methods that make API calls to Spotify's API. This required the following:

  • Mocking of the node-fetch module, which was used to make API calls
  • Checking the number of calls to the fetch method
  • Checking the inputs to the fetch method

Testing the Mongoose Models + MongoDb

Metrinom has a single Mongoose model - User. This stores user information made to use API calls and display non-sensitive user data on the frontend. User JSON objects are stored in a collection of the MongoDb database. This required the following:

  • Use of monogodb-memory-server to mock a database
  • Checking retrieval, creation and deletion of User objects from/to the database

Testing the Express API

There are numerous API endpoints that Metrinom require to function correctly. All of these endpoints were test apart from two. These two endpoints are related to user Spotify account authorization. Testing was therefore done on all the /spotify and /user endpoints. To carry out the tests, the following was required:

  • Checking the express server is configured and launched correctly with the required loaders and routes
  • Launching an express server and making fetch calls to them
  • Mocking of the User Mongoose model using mockingoose in order to create more efficient and targeted tests (ignore implementation of the Mongoose model)
  • Mocking of all methods tested in the 'Testing the vanilla JS code' section mentioned above
  • Checking the status code and mock data return from each API call
Clone this wiki locally