2
votes

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.

2
node.js and mongodb are basic components so they don't offer test/development/production configurations, you can write it yourself or find a good available framework. Can I know what framework use by your project express, sails, mean-stack, yo ...? - damphat
I use yeoman - generated angular app with express. - avanti
Maybe use a configuration file... stackoverflow.com/questions/5869216/… - dfowler7437
Yes that's good idea. I am using the configuration file. But I thought about switching database on running node.js - just not to load fixtures in to db that is used by developers and may contain useful data. My solution for now is to dump the database before running tests and restore it after. Maybe this idea of changing dbs on runtime is not the smartest... - avanti

2 Answers

1
votes

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.

0
votes

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