Skip to content

Backend

Liam edited this page Apr 28, 2020 · 8 revisions

The backend runs on NodeJS server-side JavaScript using Express as the web framework and using Mongoose as the MongoDB ORM library for interfacing with the database. Check out those links if you want to learn more about them.

Prerequisites

Please have the following software installed on your computer before following any further steps

NodeJS: Used to run our Javascript code. Note that you'll need a newer version of NodeJS (recommended v12+).

MongoDB: A NoSQL database that the application uses. Please follow all the instructions in the link provided for your specific OS.

Firebase: A cloud-based database provider that we use for authentication. You'll need your server key in order to successfully make requests to your firebase instance.

Running backend locally

  1. Navigate into se701-assignment/server in your terminal

  2. Run npm install to install the dependencies for this project

  3. Start the MongoDB database locally. You can find instructions on their website for Windows, Mac and Linux

  4. Create the /server/.env file with the following contents if it doesn't already exist:

    DATABASE_URL=mongodb://127.0.0.1:27017/
    SERVICE_ACCOUNT={YOUR_FIREBASE_SERVER_KEY}
    

    See this page for information on how to generate this key

  5. In a separate terminal tab to where you started MongoDB, run npm run start

  6. If everything is successful, you should see the following message 🎉🎉

Note: You can test any of the API endpoints using a tool like Postman or cURL

Unit tests

Overview

The backend uses the jest testing framework as well as the jest-mongodb preset. The preset allow our tests to use an in-memory MongoDB (mongodb-memory-server). As a result, we no longer are dependent on a local or deployed instance of MongoDB running when executing our tests. Additionally, it allows us to spin up multiple mongod processes (each around 7 mb) which means tests can be run in parallel (and in isolation from one another).

Running tests

Note: To ensure that the tests use the in-memory database, make sure a local instance of MongoDB is not running

  1. Run npm test

Writing tests

The backend is set up for both unit and integration tests. Unit tests are used to test if our Models are correct (i.e Is the correct field validation being performed), while integration tests are used to see if our API's are performing the their specified functionality (i.e testing the GET /posts endpoint).

  • Unit tests are can be found in /tests/model
  • Integration tests can be found in /tests/integration

For both unit and integration tests, you should include require src/db/index.js and set up the database in the test case as follows

process.env.NODE_ENV = "test";
const db = require("../../src/db");

describe("Test name", () => {
  beforeAll(async done => {
    db.connect()
      .then(() => done())
      .catch(err => done(err));
  });

  afterAll(async done => {
    db.close()
      .then(() => done())
      .catch(err => done(err));
  });


})

It is important that you include process.env.NODE_ENV = "test" as NODE_ENV is used to determine what database connection to make (in this case, we want it to be the in-memory db)

Integration

  • A integration test should verify that an API performs is specified behavior. You should check the returned status code and response body, given various different inputs.
  • Integration tests should use the supertest library. You can call any API as follows
await supertest(app).post("/posts").send(postData)

Unit tests

  • A unit test should verify that a Model behaves as expected. You should check that they can be created, retrieved, updated and deleted as expected- as well as ensuring all validation is performed correctly.

Clone this wiki locally