When starting my application it connects to MongoDB Atlas as logged, however, when running mocha tests it does not even try to connect to the DB.
here is my server.js file
require('dotenv').config()
const express = require('express');
const connectDB = require('./DB/connection')
const app = express();
app.use(express.json())
const PORT = process.env.PORT
connectDB();
app.listen(PORT, () => console.log(`Server started at ${PORT}`))
and this is the connection..js file
const mongoose = require('mongoose');
const URI = `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}#betacluster.7jf4v.mongodb.net/servicenowclone?retryWrites=true&w=majority`
const connectDB = async () => {
try {
mongoose.connect(URI, {
useUnifiedTopology: true,
useNewUrlParser: true
});
mongoose.connection.once('open',function() {
console.log('connection established');
}).on('error',() => console.log('gi atay way connection sa database man'))
} catch (err) {
console.log(err)
}
}
which logs
Server started at 3000
connection established
so I know it connects to the DB successfully, however when creating tests using mocha, it doesn't even try to connect to the DB, here is the complete test file.
const mocha = require('mocha');
const assert = require('assert');
const ticketInstance = require('../models/ticket')
//describe tests
describe('saving a ticket', function () {
it('create ticket', async function (done) {
const newTicket = new ticketInstance({
number: 1,
type: 'Request',
customer: 'Carlo Principe',
description: 'first ticket created from a test',
subject:'test subject'
})
newTicket.save().then(function (){
assert(newTicket.isNew === false);
done()
})
});
})
Am I missing something, it logs timeout exceeded and does not show the connection established console.log I created in connection.js
Thanks!
The best way is mocha --delayed switch. mocha doc says
If you need to perform asynchronous operations before any of your suites are run (e.g., for dynamically generating tests), you may delay the root suite. Run mocha with the --delay flag.
For example, use mocha in this way mocha --recursive --exit --delay --ui tdd tests.js and --delayed enable you to trigger running the root suite via calling run() explicitly.
const fn = async x => {
return new Promise(resolve => {
setTimeout(resolve, 1500, 2 * x);
});
};
(async function() {
const z = await fn(3);
suite("won't run until run() executes", () => {})
run();
})();
For more information, please read https://mochajs.org/#delayed-root-suite.
Related
I read a lot of answers to similar questions already but can't figure out what is wrong in my code.
this is my server.js file
const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
app.use(require('koa-bodyparser')())
const login = (ctx, next) => {
ctx.body = ctx.request.body
}
const router = new Router({ prefix: '/api' })
router.get('/test', (ctx, next) => {
ctx.body = { resp: 'GET REQUEST /test WORKING' }
})
router.post('/login', login)
app.use(router.routes())
module.exports = app
this is my index.js file
const server = require('./server')
server.listen(3000, () => {
console.log('App is running on http://localhost:3000')
})
and this is my mocha test file
const axios = require('axios').default
const expect = require('chai').expect
const app = require('./server')
describe('7-module-3-task', () => {
describe('test', function () {
let server
before(done => {
server = app.listen(3000, done)
})
after(async () => {
server.close()
})
it('should return response from server', async () => {
const response = await axios.get('http://localhost:3000/api/test')
expect(response.data, 'should return object with key "resp').to.have.property('resp')
})
})
})
It's working okay when I make a request in Postman. I tried multiple options already but I still get 404 response, as I understand test is performed before server started running...? How can I make it working ?
First, I would move the startup (app.listen) directly into the server.js (not critical, but maybe more simpler because the require in your test would then already start your server:
const Koa = require('koa')
const Router = require('koa-router')
const app = new Koa()
app.use(require('koa-bodyparser')())
const router = new Router({ prefix: '/api' })
router.get('/test', (ctx, next) => {
ctx.body = { resp: 'GET REQUEST /test WORKING' }
})
app.use(router.routes())
app.listen(3000); // can be a parameter but for simplicity hardcoded here
module.exports = app
In your test you then do:
let chai = require('chai');
let chaiHttp = require('chai-http');
let server = require('./server'); // this will already start up your server
describe('API Tests', () => {
describe('TEST endpoint', () => {
it('It should GET response from test endpoint', (done) => {
chai.request('http://localhost:3000')
.get('/api/test/') // be sure to have a trailing '/' !!
.end((err, res) => {
res.body.should.have.property('resp');
done();
});
})
});
});
One more hint: maybe in your original code you just have to make sure, that you have a trailing / when calling your route in the test.
Code snippets not testet but I hope you get the idea.
I shared the same code with 2 of my friends and they managed to run tests successfully.
I tested it on my other laptop after this and tests worked as well.
The problem was in the port. 3000 port was used as a default one in the debugger in Webstorm, not sure why but still.
Launching the server on port 3000 in a regular way, not in mocha tests, worked very well but in tests, it did not work, not sure why.
So for those who ever face something similar, check the default port of the debugger or any other built-in server.
I have an Express application that looks like this.
const app = express();
...
...
...
router.post(...);
router.get(...);
router.delete(...);
app.use('/api/v1', router);
MongoClient.connect(mongoUri, { useNewUrlParser: true })
.then(client => {
const db = client.db('db_name');
const collection = db.collection('collection_name');
app.locals.collection = collection;
})
.catch(error => console.error(error));
const server = app.listen(settings.APIServerPort, () => console.log(`Server is listening on port ${settings.APIServerPort}.`));
module.exports = {
server,
knex // using this to connect to the RDBMS
}
The application uses both an RDBMS and Mongo.
I wrote tests for the application using Mocha and added the following block to the Mocha test.
const app = require('../app');
...test 1...
...test 2...
...test 3...
...
...
...
...test n...
after(async () => {
await app.knex.destroy();
});
The after hook closes out my connection to the RDBMS.
However, I don't know how to close the MongoDB connection once the test finishes.
Owing to keeping this connection open, the test never exits and hangs once all the tests have been run.
The closest answer that I have been able to find is this one - Keep MongoDB connection open while running tests using mocha framework.
However, I was unable to get to work for me.
Can someone please help with this?
Update
A combination of the answers below is what solved the problem.
const mongoClient = new MongoClient(mongoUri, { useNewUrlParser: true });
mongoClient.connect()
.then(client => {
const db = client.db('...');
const collection = db.collection('...');
app.locals.collection = collection;
})
.catch(error => console.error(error));
const server = app.listen(settings.APIServerPort, () => console.log(`Server is listening on port ${settings.APIServerPort}.`));
module.exports = {
server,
knex,
mongoClient
}
We can rewrite the mongo function to make it work
const client = new MongoClient(uri);
client.connect()
.then(client => {
const db = client.db('db_name');
const collection = db.collection('collection_name');
app.locals.collection = collection;
})
.catch(error => console.error(error));
And in the after block -
after(async () => {
await app.knex.destroy();
await client.close();
});
I have implemented the following code from this link:
What is best way to handle global connection of Mongodb in NodeJs
to create a class for the connection of MongoDB. But when I try to call the singleton class in the router, I get the following error:
TypeError: Connection.db.collection is not a function
mongodb.js
const MongoClient = require('mongodb').MongoClient
const url = '...';
class Connection {
static connectToDB() {
if ( this.database ) return Promise.resolve(this.database)
return MongoClient.connect(this.url, {useNewUrlParser: true}, (err, db) => {
if (err) console.log(err);
else {
this.db = db;
console.log('MongoDB connected');
}
})
}
}
Connection.db = null
Connection.url = url
Connection.options = {
bufferMaxEntries: 0,
reconnectTries: 5000,
}
module.exports = Connection;
app.js
const express = require('express');
const app = express();
let bodyParser = require('body-parser')
// mongodb config
const Connection = require('../config/mongodb');
const server = app.listen(3000, () => {
Connection.connectToDB(); // output: MongoDB connected
console.log(`listening to port: ${port} on http://127.0.0.1:3000}/`); // output: listening to port: 3000 on http://127.0.0.1:3000/
});
router.js
const router = require('express').Router();
let Connection = require('../config/mongodb');
router.post('/data', (req, res) => {
Connection.db.collection('tweets').find({}) // output: TypeError: Connection.db.collection is not a function
.then(tweets => console.log(tweets))
.catch(err => console.log(err));
});
Try once to package.json, change mongodb line to "mongodb": "^2.2.33". You will need to npm uninstall mongodb; then npm install to install this version.
The question you linked uses promises throughout, whereas you're using the callback version of connect.
return MongoClient.connect(this.url, {useNewUrlParser: true}, (err, db) => ...
You then call this without returning in your server:
Connection.connectToDB();
console.log(`listening to port: ${port} on http://127.0.0.1:3000}/`);
There is therefore no guarantee that your connection will have been made by the time your first api request comes in. In fact, if you did:
Connection.connectToDB();
console.log(`listening to port: ${port} on http://127.0.0.1:3000}/`);
Connection.db.collection('tweets').find({});
It would fail every time as Connection.db will still be null.
In the example you linked, using Promises protect against that. Note in particular the connect method:
static connectToDB() {
if ( this.database ) return Promise.resolve(this.database)
// ** USING THE PROMISE VERSION OF CONNECT **
return MongoClient.connect(this.url, this.options)
.then(db => this.db = db)
}
and your usage code should also use promises:
return Connection.connectToDB()
.then(() => {
// do something here
});
I am using express and connect via the npm package "mongodb": "^3.0.10" to a mongodb.
My app.ts looks like this:
const app = express();
let server: http.Server;
(async () => {
app.use(bodyParser.json());
let db: Db;
try {
const host = config.get<string>("api.dbConfig.host");
console.log(host);
const dbName = config.get<string>("api.dbConfig.dbName");
const port = config.get<string>("api.dbConfig.port");
const connectionString = "mongodb://" + host + ":" + port;
const mongoClient = await MongoClient.connect(connectionString);
db = mongoClient.db(dbName);
await db.collection("users").createIndex({email: 1}, {unique: true});
}
catch (err) {
console.log(err);
console.log("can not connect to MongoDB");
}
const userRepo = new UserRepository(db);
// Routes
app.use("/v1/users", userRoutes(userRepo));
server = http.createServer(app);
server.listen(3000);
server.on("listening", () => {
console.log("listening");
});
})();
module.exports = app;
For testing i use jest and supertest. The tests run successfully, but they never end, because there are still connections to mongodb.
The tests look something like this:
describe("user routes", function () {
it("should return all users", async () => {
const response = await agent(app).get("/v1/users/");
expect(response.status).to.be.equal(200);
expect(response.body).to.be.an("array");
expect(response.body).to.have.lengthOf(2);
});
I understand, that the mongodb driver uses connection pooling and the way i pass the db- (or collection-) object to my user repository, makes it impossible to close the connections manually in a test scenario.
I guess a need a better way a pass the db connection to my user repository, but i can not think of a better, or more decoupled way at the moment.
Try await mongoClient.close() after your tests are done. See MongoDB docs. As far as I know, Jest supports before() and after() hooks, and I imagine before() and after() hooks support async/await like Mocha's do.
I am testing an nodejs-app with a server and a client component on nodejs 8.9 with mocha.
For mocha to end properly, I have to make sure that all socketio and http-servers are closed after the tests have been run. This works fine with normal tests, but as soon as I register a middleware to the socketio-server, the mocha-process won't close and stay open forever.
Testcode (comment in the second test to see the problem, run via mocha test.spec.js):
// test.spec.js
'use strict'
const Express = require('express')
const Http = require('http')
const ioserver = require('socket.io')
const ioclient = require('socket.io-client')
const NODE_PORT = process.env.NODE_PORT || 3000
describe('Client', function () {
beforeEach(() => {
const express = new Express()
this._http = Http.Server(express)
this._ioserver = ioserver(this._http)
this._http.listen(NODE_PORT)
})
// this test works perfectly, even when I copy it and run it
// multiple times in this suite
it('should connect to a socketio-server', (done) => {
this._ioserver.on('connection', () => {
client.close()
done()
})
const client = ioclient.connect(`http://localhost:${NODE_PORT}`)
})
// this test also finished, but the suite hangs afterwards - as if
// a socket-client or socket-server was not closed properly.
it('should finish the test suite even with a middleware', (done) => {
this._ioserver.use((socket, next) => {
return next()
})
this._ioserver.on('connection', () => {
client.close()
done()
})
const client = ioclient.connect(`http://localhost:${NODE_PORT}`)
})
afterEach(() => {
this._ioserver.close()
this._http.close()
})
})
Any ideas why that happens?
So, the problem was, that the server closed the client connection on a successful connection event. The client did not get any information on that, but instead saw a failed connection and tried to reconnect. This opened a socket to the server again and because the server was already closed, the connection error kept coming.
This behavior stopped node from properly destroying all objects, which in turn explaines the hanging. The solution is to call done() only after the client has declared a connection open, not after the server has declared a connection open like so:
'use strict'
const Express = require('express')
const Http = require('http')
const ioserver = require('socket.io')
const ioclient = require('socket.io-client')
const NODE_PORT = process.env.NODE_PORT || 3000
describe('Client', function () {
beforeEach(() => {
const express = new Express()
this._http = Http.Server(express)
this._ioserver = ioserver(this._http)
this._http.listen(NODE_PORT)
this._client = null
})
it('should connect to a socketio-server', (done) => {
this._ioserver.on('connection', () => {
done()
})
this._client = ioclient.connect(`http://localhost:${NODE_PORT}`)
})
it('should finish the test suite even with a middleware', (done) => {
this._ioserver.use((socket, next) => {
return next()
})
this._client = ioclient.connect(`http://localhost:${NODE_PORT}`)
// if we wait for the server and kill the server socket,
// the client will try to reconnect and won't be killed
// by mocha.
this._client.on('connect', () => {
done()
})
})
afterEach(() => {
// this last call forces the client to stop connecting
// even if tests failed
this._client.close()
this._ioserver.close()
this._http.close()
})
})