lundi 1 octobre 2018

Set postgres database url on travis-ci for testing node.js application

I have written some tests for my node.js application and the tests are running locally using a Postgresql test database.

When I run my test script, npm run test, the environment is set to test and when this happens, the database connection string is set for the test database and my queries in the application are now done on the test database. Like so:

let connectionString;
if (process.env.NODE_ENV === 'test') {
  connectionString =`postgresql://${process.env.DB_USER}:${process.env.DB_PASS}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.TEST_DB_NAME}`;
} else {
  connectionString =`postgresql://${process.env.DB_USER}:${process.env.DB_PASS}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`;
}

This way my tests are being run on the test database.

On travis however, I know I am going to need to further configure its own database. On the travisCI docs, I read about how I could set up a PostgreSQL database here, but this doesn't help me because how do I get the full database URL as above? On travisCI, what am I to use as my database hostname or port and how do I set this value inside my code?

How do I set the database connection string and access it in my code?

Thanks for any ideas.

Aucun commentaire:

Enregistrer un commentaire