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?

What are we building?

What do we need?

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

// 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

Development Mode

yarn run dev

Test Mode

yarn run test

Production Mode

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

Conclusion

Developer Circle Lusaka

This is the official medium Publication for Developer…