avanti
avanti

Reputation: 21

Is it possible to switch database to test database on node.js?

I want to run e2e tests like protractor (or other Selenium tests) on developement server. Is it possible to switch to different - test database for the testing time? I am loading fixtures before each test runs. What are good practices for this kind of testing - with node.js and mongo.db in backend, concerning database setup?

Thank you in advance.

Upvotes: 2

Views: 2603

Answers (2)

andrew boddy
andrew boddy

Reputation: 1

Mocha can now accept a --config file which could be used to point to a different database. I use the same database server, a server can have multiple databases, this makes it very simple and lightweight for a developer.

https://www.w3resource.com/mocha/command-line-usage.php

Upvotes: 0

Jacek Ciolek
Jacek Ciolek

Reputation: 428

The easiest way to do it IMHO would be to spin up another instance of your application with different configuration, namely connecting to a different database and listening on a different port. Then you can point Selenium to it. In theory the FE of the application should be port agnostic, however if that presents a problem, nginx can be of a great help.

Let's consider you want it on port 3333 and domain test.myapp. Here is sample configuration file for nginx.

server {
    listen 80;
    server_name test.myapp;

    location / {
            proxy_pass http://localhost:3333;
            proxy_set_header Host $host;
            proxy_buffering off;
    }
}

Of course you would like to have another server defined for your current development server. Simply rinse and repeat.

Usually the configuration in a nodejs application is chosen based on the value of environmental variable NODE_ENV. You can pass it like so, when you run your app (I am assuming here it is a Linux server):

$ NODE_ENV=test node app.js

Then inside your application you would easily get access to it:

var env = process.env.NODE_ENV

I hope it helps.

Upvotes: 1

Related Questions