Real-world Node.JS TDD example

Pierre Criulanscy
Spotlight On JavaScript
17 min readSep 7, 2020

--

A walkthrough of a real-world example of applying TDD to develop a Node.JS lambda function. Featuring graphQL API call and upload to an Amazon S3 Bucket.

The long TDD journey ahead — Photo by Vlad Bagacian on Unsplash

I was recently asked to develop a little lambda function to periodically generate XML sitemaps and upload them to an Amazon S3 Bucket. I thought it was a perfect example of a TDD walkthrough. The scope is broad enough to cover interesting use cases and showcase some unit testing patterns.

Who should read this post ?

This post is not intended for TDD beginners. We’re going to take some shortcuts, and it is not idiomatic TDD, just an opinionated version of it. However, you don’t need to be a TDD expert, this post is for you if:

  • you already played with TDD and struggle to use it in a real project
  • you write tests that keep breaking every time you make the slightliest change and want to know how to write less brittle tests
  • you struggle with testing I/O (web API calls, saving to a database, uploading file, etc.)
  • you didn’t find an alternative to “interface” in javascript but still want to write maintainable and easy to test code
  • you’re wondering how to use both outside-in and inside-out TDD techniques

--

--