/booking-tdd.js

Look, no Mocks! Functional TDD with JavaScript (Node.js)

Primary LanguageJavaScript

Booking Demo; functional TDD attempt

My previous attempt with PHP.

Challenge

  • Decoupling decisions from effects

    Solution: The world is impure. Instead, we strive towards implementing as much of our code base as pure functions, so that an application is impure only at its boundaries. Using functional composition with monads, we can succinctly conditionally compose from pure and impure functions.

    Benefits: Pure functions only makes a decision based on input, and returns information about this decision as output, so is easy to unit test. Impure functions are used as humble functions that you may don't need to unit test. Impure functions can call pure functions, so at the boundary, an application must gather impure data, and use it to call pure functions. This automatically leads to the ports and adapters architecture.

History

From Dependency injection to dependency rejection video by Mark Seemann
From dependency injection to dependency rejection text by Mark Seemann
Test-Driven Development with F# course by Mark Seemann
Dependency Rejection and TDD without Mocks video by Антон Молдован

Packages

Requirements

To run this application, you will need:

Installation

npm install

Usage

npm run start
npm run test

Environment variables

See .env file.

APP_CAPACITY = 10
APP_DATABASE = 'memory'
NODE_ENV = 'production'
HTTP_HOST = 'localhost'
HTTP_PORT = 80
MONGODB_URL = 'mongodb://localhost:27017/booking?connectTimeoutMS=300'
JSONSCHEMA_ASYNC_VALIDATE = true

Example request

curl -X POST http://localhost/reservations/ \
 -H 'Content-Type: application/json' \
 -d '{"date": "Y-m-d H:i:s", "name": "Some name", "email": "Some email", "quantity": 1}'