Automation Tests
4.1 Objective
The goal of running the test is to prevent potential errors before the code ships. Mocha is an automation tool from JS
4.1 Setup
In configuration file config/grapgDB.js, load different config file configTest for graphDB. Also, we use different
repository cmmpTest
async function createRepository() {
const form = new FormData();
if (process.env.test)
form.append('config', fs.createReadStream(__dirname + '/configTest.ttl'));
else
form.append('config', fs.createReadStream(__dirname + '/config.ttl'));
form.append('location', '');
const res = await fetch(graphdb.addr + '/rest/repositories', {
method: 'POST',
body: form,
headers: form.getHeaders()
});
if (res.status === 500) {
throw Error('Fails to create repository: ' + (await res.json()).message);
}
}
For configuration in server/config/index.js, we user different route /cmmpTest instead
mongodb: {
addr: `mongodb://${isDocker ? 'host.docker.internal' : 'localhost'}:27017/cmmp` addr: `mongodb://${isDocker ? 'host.docker.internal' : 'localhost'}:27017/${process.env.test ? "cmmpTest" : "cmmp"}`
}
4.2 Test file configuration
To set up the test environment, we import the chai npm packages and configuration
process.env.test = 'true';
const app = require('../loaders/express');
const chai = require("chai");
const chaiHttp = require("chai-http");
const {expect} = chai;
chai.use(chaiHttp);
const config = require('../config');
Also, start the server beforehand, we need to make sure that graphDB and mongoDB reset before testing.
before('Start server', async function () {
this.timeout(20 * 1000);
// Speedup hashing in test.
config.passwordHashing.iterations = config.passwordHashing.iterations / 100;
await require('../loaders/graphDB').load();
const {db} = require("../loaders/mongoDB");
// Remove all data from MongoDB
await db.dropDatabase();
console.log('Server Started!')
});
4.3 Test format
Format of the test
const chai = require("chai");
const {expect} = chai;
describe("test example", function () {
it("test example 1" , async () => {
...
})
})
In it The following is to check whether the result is expected
it("get the result", async () => {
...
expect(resVar).eq(var)
expect(res).to.have.status(200);
})
4.4 End-to-End Test
Test whether the api cause error or not
The following is an example
describe("Server!", () => {
const agent = chai.request.agent(app);
it("welcomes user to the api", async () => {
const res = await agent.get("/");
expect(res).to.have.status(200);
});
});
4.5 Integration Test
Test the functionalities and correctness
The following is an example
it("Validates Users", async () => {
for (let i = 0; i < tokens.length; i++) {
await User.verify_token(tokens[i])
}
const numberOfUsersInMongoDB = (await MongoDBUserModel.find({})).length
const numberOfUsersInGraphDB = (await User.all()).length
// After the validation, it will saves the user to GraphDB
expect(numberOfUsersInMongoDB).eq(numberOfUsersBeforeInMongoDB + numberOfUsers)
expect(numberOfUsersInGraphDB).eq(numberOfUsersBeforeInGraphDB + numberOfUsers)
})