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
- Start a local MongoDB instance from the command line (instructions)
- In a different terminal session,
cd
to3Lancers/telos-backend
- Execute
npm ci
to install dependencies - Run the backend with
npm run start
- If successful, you should see this:
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
- Ensure that a local MongoDB instance is not running
cd
to3Lancers/telos-backend
- 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);