How To Write An Express JS Server Using Test Driven Development

Part One: Setting Up Environments

Mbuyu Makayi
Feb 10, 2019 · 9 min read
courtesy of freecodecamp

Why TDD In First Place?

So you have probably been in this situation before, you write software. It is working beautifully — but just hours before that big presentation. You decide to make a few changes to a few methods. You run the application, still works — or so you think.

What are we building?

Now in this tutorial am going to show you how you can build and test your Express JS Server using the Test Driven Development approach.

What do we need?

  1. Express JS — Most popular Node JS Server framework.
  2. Config— Helps setting up environments (development, test, and production).
  3. Debug — Only show logs in test or development environments.
  4. Mongoose — An elegant ODM for MongoDB in Node JS.
  5. Mocha — Testing framework.
  6. Nodemon — Restarts our development server automatically.
  7. Chai — Our TDD/BDD assertion library.
  8. Babel CLI — A Javascript compiler.
  9. SuperTest — HTTP server testing library.
  10. Yarn — Fast, Reliable, & Secure Dependency Manager ( you are free to use npm instead, just replace the commands with npm equivalent)

Setting Up Stuff

1. mkdir movie_server ; cd movie_server2. yarn init3. yarn add  mocha chai debug supertest nodemon babel-cli babel-preset-node8 config --dev4. yarn add express mongoose 5. mkdir dist //when you run yarn build,prod files will be stored here 6. mkdir tests // mocha will look for tests here.
7. mkdir src ; cd src ; touch index.js &
"build": "babel src -d dist --source-maps"
"serve":"NODE_ENV=production node dist/index.js",
"dev":"DEBUG=server:debug NODE_ENV=development nodemon src/index.js --exec babel-node"
"test":"DEBUG=server:debug NODE_ENV=test mocha --require babel-core/register --reporter spec --exit   tests/ "

Let’s Setup Our Environments Using Config

Remember that we have three environments our application will be running in and that each will have its own variables, let’s set up each one of them which will be used by the package config. This again is important because it helps to make sure that we configure our server to use certain settings depending on the environment. For example, we can configure our server to use the development database when we run our server in development and the production database when in production mode.

// configs/development.json
{
"name": "Server Developement","database": "mongodb://localhost:27017/development","port": 3000}// configs/test.json
{
"name": "Server Test","database": "mongodb://localhost:27017/test","port": 3100}// configs/production.json
{
"name": "Server Production","database": "mongodb://localhost:27017/production","port": 3000}

Testing The Environments

Now let’s see that our environments are working accordingly. To do so we are going to create an express js server in the index.js with the following code.

Development Mode

In the first line, I configure the imported debug function to only show its log when the DEBUG environmental variable is equal to ‘server:debug’. Am also using config.get() to get the value of the key from the JSON files we set up in the config folder. The way config works by only loading the appropriate JSON file whose name matches the NODE_ENV’s value.

yarn run dev

Test Mode

Next, we will test that our server running is able to run in test mode. But unlike the other modes which just starts the server, here will be also be running tests using Mocha.

yarn run test

Production Mode

The last environment we going to test that it is working is the production environment but before we run yarn run serve, we need to first run yarn run build. The tells babel to compile our code into ES5 syntax and transfer the resulting files into the dist folder.

yarn run build
yarn run serve
console.log(`server is running on port ${config.get('port')} and in ${config.get('name')} mode`);

Conclusion

So for this part of the tutorial, we have covered what TDD is, how it can help you make changes to your code with confidence and avoid those embarrassing moments. We have also covered how to set up your project with the necessary packages and also covered how to setup up three environments for your server to run in. In the next of the tutorial, I will cover how to now write your MongoDB Schemas and server APIs using TDD with SuperTest and Chai.

Developer Circle Lusaka

This is the official medium Publication for Developer Circles Lusaka

Thanks to Olivier J.M

Mbuyu Makayi

Written by

Senior Software Engineer at The ZIG| Facebook Developer Circle:Lusaka Community Lead | Amateur Photographer | Fullstack Javascript

Developer Circle Lusaka

This is the official medium Publication for Developer Circles Lusaka

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade