-
Notifications
You must be signed in to change notification settings - Fork 17
Home
By Grant Steinfeld grant.steinfeld@ibm.com
This code pattern showcases test driven development(TDD) methodologies when designing and developing a Node.js microservice based application. To illustrate these methodologies we will be using a world class currency exchange microservice, built with modern JavaScript.
- Node.js
- Continuous Delivery
- Microservices
TDD is a style of programming that closely intertwines coding, testing, and designing. To illustrate, when designing the functionality of your application, you would write unit-tests first then implement the code afterwards.
This code pattern will show you how to create a world class currency conversion microservice using Test Driven Development (TDD) in Node.js. This code pattern is a microservice that is a part of the Bee Travels project
- Develop using the Test Driven Development (TDD) methodology
- Incorporate tests throughout the development lifecycle - deploy lifecycle will make your life easier, coding fun and be confident that your application will run as best as possible even after code changes due to either new feature requests or bug fixes are requested or found in QA
- Write test first that break - philosophy
- Design and create a microservice with a REST interface that is documented with a test harness automatically provided by OpenAPI connect aka Swagger definitions, by just adding a simple swagger.yaml file!
- Use this simple microservice application as a basis to create awesome world class microservices using Node.js and the latest version of ECMA Script
It is during coding ( aka Design time ) that TDD is practiced.
The Red-Green-Refactor process is the core part of TDD, without it no other aspect of TDD will function.
Figure 1 below showing the steps that typically occur when working in a test driven way (aka Red-Green-Refactoring)

figure 1: Red-Green-Refactoring
- Pick a story ( e.g. feature request or bug/issue )
- Write a unit-test that represents the story
- Run the test, it will fail (RED)
- Implement business logic towards making this test to pass
- Run the test until it passes (GREEN)
- Refactor business logic to improve code (TEAL)
The name comes from the status of the tests within the cycle. When in the red state, code does not work. When in the green state everything is working, but not necessary in the most optimal way. When in the teal phase we are refactoring phase where we are confident our code is covered with tests and thereby gives us the confidence to change and improve our code.
This flow is for the runtime of the currency conversion microservice.

figure 2: production flow
- Consumer calls the microservice over the internet (http/s request)
- ExpressJS
web serveraccepts the REST request (e.g. GET /convertCurrency/ZAR/USD/600.66) - Code routing in Express passes the request to a service module which in turn calls the European Currency Exchange API
- An exchange rate for ZAR is retrieved and stored. The value of 600.66 South African Rands (ZAR) is converted to US Dollars(USD)
- The ExpressJS
web serversends a response to the calling Consumer with the dollar amount ( e.g. $40.59 )
- Clone the repo by running
git clone TDD-NodeJS-Containers - Install packages with NPM by running
npm install - Start the app by running
npm start - Browse the API from your browser
localhost:4001
- JavaScript / Node
Using Test-Driven Development for Microservices by Bill Doerrfeld