This is my test code. I'm testing an API. The problem is the "after" hook is working and dropping database after the test ends. But the "before" hook is not working. what's the issue here? I tried but unable to find out the issue. I tried to run the before hook with only one dummy test like logging something in the console. Didn't work either.
const chai = require('chai');
const { assert } = require('chai');
const chaiHttp = require('chai-http');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
require('../resolvedir');
const User = require('models/User');
const server = require('bin/www');
const testData = require('./test_data');
chai.use(chaiHttp);
describe('Empty User Collection before test', function () {
it('should drop User Collection before test starts', function () {
before(function (done) {
User.collection.drop();
done();
});
});
});
describe('Testing /registration end point', () => {
it('should return a valid JWT token', (done) => {
chai.request(server)
.post('/register')
.send({ name: testData.name, email: testData.email, password: testData.password })
.end((err, res) => {
assert.equal(res.status, 200, 'Http response code is 200');
assert.exists(res.body.auth, 'Auth confirmation message exist');
assert.isTrue(res.body.auth, 'Auth confirmation message is true');
assert.exists(res.body.token, 'JWT token is neither null or undefined');
assert.isString(res.body.token, 'JWT token is string');
done();
});
});
it('should fail registration', (done) => {
chai.request(server)
.post('/register')
.send(testData)
.end((err, res) => {
assert.equal(res.status, 409, 'Http response code is 409');
assert.isString(res.body.message);
assert.equal(res.body.message, 'User Exist');
done();
});
});
});
describe('Testing /login end point', function () {
it('should get a valid JWT token on successful login', function (done) {
chai.request(server)
.post('/login')
.send({ email: testData.email, password: testData.password })
.end((err, res) => {
assert.isString(res.body.token, 'JWT token is string');
done();
});
});
});
describe('Empty User Collection after test', function () {
it('should drop User Collection after test ends', function () {
after(function (done) {
User.collection.drop();
done();
});
});
});