I have few test cases to test the REST API and everything is in Typescript. I am also using docker to dockerize my application. I have 3 containers which are REST_API_app, MongoDB, api_testing_container. I create the container api_testing_container once other two are created and it uses the same image that is build for REST_API_app. Also I've seen lot of posts about increasing the timeout of mocha using --timeut but that is not what I am looking for and it also does not work in my code, I've tried it. My test case file looks like this :
import { expect } from 'chai';
import request from 'supertest';
import app from '../app';
import * as mongodb from '../mongo/connection';
describe('Testing the API', () => {
before((done) => {
mongodb.connectMock()
.then(() => done())
.catch((err:any) => done(err))
})
it('OK, adding new employee', (done) => {
request(app).put('/add')
.send(<some JSON data>)
.then(res => {
...some matcher
done();
})
.catch(err => done(err));
})
it('OK, getting all employees', (done) => {
request(app).get('/all')
.then(res => {
...some matcher
done();
})
.catch(err => {
done(err)
});
})
it('OK, getting employee by ID', (done) => {
request(app)
.get(`/create/${id}`)
.then(res => {
...some matcher
done();
})
.catch(err => done(err));
})
})
When I try to run the above test cases it perfectly runs on local machine without docker, but when I create all three containers with docker-compose it shows the following error in test container
api_testing_container | Testing the API
1) "before all" hook for "OK, adding new employee"b
api_testing_container |
api_testing_container |
api_testing_container | 0 passing (20s)
api_testing_container | 1 failing
api_testing_container |
api_testing_container | 1) Testing the API
api_testing_container | "before all" hook for "OK, adding new employee":
api_testing_container | Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/usr/src/app/src/test/employee.spec.ts)
api_testing_container | at listOnTimeout (internal/timers.js:549:17)
api_testing_container | at processTimers (internal/timers.js:492:7)
My mongodb connection for mock looks like this. It seems the error is in this file when I put console.log("test") before mongoose.connect() it does not print it.
import mongoose from 'mongoose';
import {Mockgoose} from 'mockgoose';
export let connect = () => {
return new Promise((resolve, reject) => {
const uri: string = 'mongodb://mongo:27017/rest_api_container';
mongoose.connect( uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('MongoDB Connected');
resolve();
})
.catch(err => {
console.log(err)
reject();
});
})
}
export let connectMock = () => {
return new Promise((resolve, reject) => {
let mockgoose: Mockgoose = new Mockgoose(mongoose);
mockgoose.prepareStorage()
.then(() => {
const uri: string = 'mongodb://mongo:27017/rest_api_container';
mongoose.connect( uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then((res) => {
console.log('Mock MongoDB Connected');
resolve(res);
})
.catch((err :any)=> {
console.log(err)
reject(err);
});
})
.catch((err: any) => {
console.log(err);
reject(err)
})
})
}