Talk to people, good afternoon.
So, I'm doing some testing on my Node.js API using Mocha and chai, however I have a middleware that checks my user's token so he can get access to some HTTP requests.
The login test works fine, but when I have to do tests with the middleware-protected routes it does not work.
process.env.ESTACAO_HACK_ALUNOS_DATABASE_MODE = "test";
process.env.ESTACAO_HACK_ALUNOS_DATABASE_HOST = "localhost";
const chai = require('chai');
const expect = chai.expect;
const chaiHttp = require('chai-http');
const mongoose = require('mongoose');
const Student = require('../models/student');
chai.use(chaiHttp);
const app = require('../server.js');
let activeStudent = new Student({
name: "Active Student",
email: "[email protected]",
cpf: "192.834.586-11",
picture: "public/images/gas.jpg-1532709990908",
resume: "public/docs/docteste.pdf-1532709991846",
description: "Descrição básica",
group: "Turma 1",
isWorking: false,
birthDate: "01/09/1998",
active: true
});
let inactiveStudent = new Student({
name: "Inactive Student",
email: "[email protected]",
cpf: "012.345.678-99",
picture: "public/images/ney.jpg-1532709990908",
resume: "public/docs/DOCUMENTO TESTE PDF.pdf-1532709991846",
description: "Descrição básica",
group: "Turma 1",
isWorking: false,
birthDate: "01/09/1998"
});
before((done) => {
mongoose.connection.collections.students.drop(() => {
console.log("[Admin Test START] Cleared 'students' collection");
activeStudent.save()
.then((activeStudent) => {
activeStudent = activeStudent;
inactiveStudent.save()
.then((inactiveStudent) => {
console.log("[Admin Test START] Students added");
inactiveStudent = inactiveStudent;
done();
})
})
});
});
describe('Admin', () => {
describe('Get Students', () => {
it('All', (done) => {
chai.request(app)
.get('/students')
.then(response => {
expect(response).to.have.status(200);
expect(response.body).to.be.an('array');
expect(response.headers.authorization);
expect(response.body.length).to.be.equal(2);
done();
})
.catch(err => {
return err;
});
});
it('Inactive', (done) => {
chai.request(app)
.get('/students/inactive')
.then(response => {
expect(response).to.have.status(200);
expect(response.body).to.be.an('array');
expect(response.body.length).to.be.equal(1);
done();
})
.catch(err => {
return err;
});
});
it('Active', (done) => {
chai.request(app)
.get('/students/active')
.then(response => {
expect(response).to.have.status(200);
expect(response.body).to.be.an('array');
expect(response.body.length).to.be.equal(1);
done();
})
.catch(err => {
return err;
});
});
});
});
In this test, I would have to return the students, but when I run the test I get the following message:
1) Admin Get Students All: Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done ()" is called; if returning to Promise, ensure it resolves. (/ home / raphaelmelo / Desktop / hack-students-api / test / adminTests.js)
2) Admin Get Students Inactive: Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done ()" is called; if returning to Promise, ensure it resolves. (/ home / raphaelmelo / Desktop / hack-students-api / test / adminTests.js)
3) Admin Get Students Active: Error: Timeout of 10000ms exceeded. For async tests and hooks, ensure "done ()" is called; if returning to Promise, ensure it resolves. (/ home / raphaelmelo / Workspace / station-hack-students-api / test / adminTests.js).
All other tests that do not depend on the token pass normally.