2

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.

morten.c
  • 3,414
  • 5
  • 40
  • 45
avanti
  • 21
  • 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 Jan 03 '14 at 21:16
  • I use yeoman - generated angular app with express. – avanti Jan 04 '14 at 22:19
  • Maybe use a configuration file... http://stackoverflow.com/questions/5869216/how-to-store-node-js-deployment-settings-configuration-files – dfowler7437 Jan 05 '14 at 01:16
  • 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 Jan 06 '14 at 21:46

2 Answers2

1

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.

Jacek Ciolek
  • 428
  • 4
  • 5
  • Thank you for an answer. I am using heroku for hosting the app. So that would mean creating another application along with db. To be true I hoped this is not the only solution. – avanti Jan 04 '14 at 22:22
  • Hmmm... I am a bit confused. I thought the idea was to use another database? Anyway, usually the dev servers (either boxes or just vhosts) are separate from test servers, so that you can test particular branch, for example release candidate, while the development is still going on. – Jacek Ciolek Jan 09 '14 at 22:41
  • We have only two deployment environments currently: dev and production. So after thinking it through I see two solutions: adding test server as you suggest, where integration and e2e tests can run or running everything on dev server. In second case I dump database before integration tests and restore after. My first idea was to use different database only for tests which is aparently not possible (for e2e tests). – avanti Jan 11 '14 at 09:46
0

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