Backend - UOA-SE701-Group3-2021/3Lancers GitHub Wiki

The Telos backend server runs on the Express framework which is based on Node.js. Persistence is handled with a MongoDB implementation through the Mongoose ORM library.

Prerequisites

Node.js: v14.16.0 was used during development.

MongoDB: follow the installation and configuration instructions here.

Running the backend

  1. Start a local MongoDB instance from the command line (instructions)
  2. In a different terminal session, cd to 3Lancers/telos-backend
  3. Execute npm ci to install dependencies
  4. Run the backend with npm run start
  5. If successful, you should see this:

image

Testing

Testing is done using the Jest framework combined with an in-memory MongoDB instance (mongodb-memory-server). This allows testing to be done without needing to run a local database instance.

Running tests

  1. Ensure that a local MongoDB instance is not running
  2. cd to 3Lancers/telos-backend
  3. Execute npm run test

Writing tests

Both unit and integration tests are already set up with the former testing the data models (test/models), and the latter testing API endpoint behaviour (test/integration).

Unit tests

These should check that a data model behaves as expected, which involves verifying the CRUD operations and validating the data. To configure the database, make sure to include:

const dbHelper = require('../db-helper');

beforeAll(async () => await dbHelper.connectToDb());

afterEach(async () => await dbHelper.clearDb());

afterAll(async () => await dbHelper.closeDb());

Integration tests

These should check that an API endpoint behaves correctly, by verifying the returned status code and response body. To configure the database, make sure to include:

const { MongoMemoryServer } = require('mongodb-memory-server');
const express = require('express');
const mongoose = require('mongoose');

beforeAll(async (done) => {
  mongod = new MongoMemoryServer();

  const connectionString = await mongod.getUri();
  await mongoose.connect(connectionString, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useFindAndModify: false,
  });

  app = express();
  app.use(express.json());
  app.use('/', routes);
  server = app.listen(0, () => {
    port = server.address().port;
    done();
  });
});

afterAll((done) => {
  server.close(async () => {
    await mongoose.disconnect();
    await mongod.stop();

    done();
  });
});

API calls should be made using axios, like so:

await axios.post(url, body);