Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Grant Steinfeld edited this page Jan 28, 2020 · 4 revisions

Code Pattern

Test Driven Development (TDD) in Node.js

By Grant Steinfeld grant.steinfeld@ibm.com

get the code

Summary

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.

Technologies

  • Node.js
  • Continuous Delivery
  • Microservices

Description

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

When you have completed this code pattern, you will understand how to:

  • 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

Design time Flow

It is during coding ( aka Design time ) that TDD is practiced.

The Red-Green-Refactor process

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)

design time flow red green refactoring

figure 1: Red-Green-Refactoring

  1. Pick a story ( e.g. feature request or bug/issue )
  2. Write a unit-test that represents the story
  3. Run the test, it will fail (RED)
  4. Implement business logic towards making this test to pass
  5. Run the test until it passes (GREEN)
  6. 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.

Runtime Flow

This flow is for the runtime of the currency conversion microservice.

run time flow

figure 2: production flow

  1. Consumer calls the microservice over the internet (http/s request)
  2. ExpressJS web server accepts the REST request (e.g. GET /convertCurrency/ZAR/USD/600.66)
  3. Code routing in Express passes the request to a service module which in turn calls the European Currency Exchange API
  4. An exchange rate for ZAR is retrieved and stored. The value of 600.66 South African Rands (ZAR) is converted to US Dollars(USD)
  5. The ExpressJS web server sends a response to the calling Consumer with the dollar amount ( e.g. $40.59 )

Steps to run this code pattern

  1. Clone the repo by running git clone TDD-NodeJS-Containers
  2. Install packages with NPM by running npm install
  3. Start the app by running npm start
  4. Browse the API from your browser localhost:4001

Runtimes

  • JavaScript / Node

Related Links

Using Test-Driven Development for Microservices by Bill Doerrfeld

Test-Driven Java Development, Second Edition: Invoke TDD principles for end-to-end application development, 2nd Edition by Farcic, Viktor