Use a grunt and mocha test database

I am creating a web application in Node.js, Express and MongoDB using Mongoose. I want to have a dedicated database when I run my Mocha tests with Grunt so that I don't mess up with the database I'm using for development. How should I do it?

I currently have development database configuration information in the /config/db.js file that is loaded and connected to my development database in my app.js file on startup. How do I run my Mocha tests that run in a Grunt task, dynamically use the test database when Grunt starts? I tried to disconnect from the development database in my test files in the before () cache in the Mocha test files and then connect to the test database. However, it continues to use the development database. An example is as follows:

before(function(done) {
    if(mongoose.connection.db) mongoose.connection.close();
    mongoose.connect(<test_db_uri>, done);



source to share

1 answer

Your question is close to the next question a test environment in a Node.js / Express application .

Basically you need to use an env variable (like "NODE_ENV") to access it with process.env.NODE_ENV

, and base by value calls the correct config file. You should take a look at grunt-express-server which helps you a lot in setting up your environement.

I hope this helps!



All Articles