David Sergey Grigoryan

JavaScript Basics with BDD in Mind

Introduction

The following course addresses a few pain points that a lot of teams had in our experience when they are migrating to Node.js Micro-Services from technologies such as PHP (Laravel, Wordpress), Ruby on Rails, and Java.

  • There is a way to use JavaScript as if it’s a clone of Ruby, Java, or PHP, but it’s far from the efficient way. Instead, they use JavaScript as if it’s previous technology, and accumulate technical debt.
  • Developers need to understand underlying JavaScript Event-Loop, and how to work with async methods, whether it’s Promises or Async/Await and how to chose which one to use.
  • A lot of courses omit testing, which arguably is one of the highest priority requirements for teams who migrate to new technology.

The following course aims to give teams a rundown on how to write Test-First idiomatically correct JavaScript.

Course Plan

Part I - Chapter 1 - Ice Cream

Create New Project and Install Cucumber

Length: <10 minutes. We will create a new project and install the most important dependencies - cucumber and chai. We will go through the basics of due-diligence on how to make sure which version of dependency to install, by checking npmjs.org website, and corresponding repository, page, and continuous integration environments. Finally, we will write the first sanity test, to make sure that the installation is working.

Goal: Make sure that team understands how to create a new Node project if one is not available in the repo, and minimal actions when it comes to installing dependencies.

Dissect Ice Cream

Length: <25 minutes. Let’s write some example of how and Ice Cream will look in the code, what are the basics, and how should we name them? We will define an MVP of Ice Cream - being a Cone with a list of Scoops, and decide on how to best represent it in JavaScript.

Once it’s done we can start writing our high-level test, on how to test if something has sufficient information to be ice cream.

Goal: Get the team to discuss the basic requirement, and jump into the code by writing first Feature file, and Step Definition, and obviously validating that test framework is running by failing the first test.

Implement Scoops

Length: <20 minutes. Now that we have a high-level user story, we will implement a subtask, and write a test to make sure that we understand what is Scoop of Ice Cream, and what properties do we want it to have.

Goal: Working with basic (flat) objects in JavaScript. While in many other languages like Scoop would become a class, we want developers to be accustomed to using objects. JavaScript is a functional programming language, and it means that it has segregation between functions and data, and data is recommended to be kept as simple Objects and Lists.

Implement Ice Cream Cone with Scoops

Lengths: <20 minutes. Create an Ice Cream cone that can contain multiple scoops, we will update our test, and see how to write tests around edge cases, what if there are too many scoops?

Goal: Build on the previous lesson, we will work with complex objects, and see how to test trees of data.

Change Requests

Length: <20 minutes.

We have change request from a business, we have to stay competitive, and we need to add diversity to our Ice Creams to become a Starbucks of Ice Cream! We will add toppings to the cones, and let people chose different kinds of cones.

Goal: Get developers to think on how to update existing tests with a change request, and build on the complexity of the objects.

Price Calculator

Length: <20 minutes.

Build a new process, - Calculate the price of the Ice Cream depending on the number of features, and put the ice-cream in the right pricing tier.

Goal: Strength of JavaScript and Node.js, in particular, is the fact that it can allow us to create processing pipelines. Unlike MVC languages, JavaScript aims to provide a modular and swappable pipeline for data. In this tutorial, we will create the first segment.

Part I - Chapter 2 - Ice Cream Trucks

Discount Codes

Length: <20 minutes. Before we start with the trucks, we will quickly revisit what we did last time, and add new functionality, and refresh the memory. We will add new functionality, and a new step to the data processing pipeline - to apply a random discount.

Goal: It’s proven that people learn best when they repeat what they learned in the last lesson. On top of that - we will introduce stubbing to our test, and stub Random function in JavaScript: Math.random.

Evaluate Trucks - What is a Truck?

Lenth: <20 minutes. Our glorious leaders decided to reach full vertical integration in Ice Cream business, just like Amazon we don’t want to be simply providers of commodity, but also provide fulfilment, and as a first step, we shall acquire trucks that will serve as Ice cream Trucks.

Let’s define how a Truck will look in our data model, and start creating mocks for our tests.

Goal: Repeat what we’ve learned about objects, and how to work with complex objects, and how to work with them in idiomatic JavaScript.

Truck Evaluation

Lengths: <30 minutes. Let’s create a simple Truck Evaluation algorithm, that will create a new evaluation for a given truck. Let’s expand our tests to understand different cases.

Goal: Give developers some practice time, to write tests and logic without interrupting them for too long, to solidify what they’ve learned so far.

Evaluate Trucks - What’s not a Truck?

Length: <30 minutes. Now that we know what is a truck, let’s write tests that will screw with our evaluation by providing them with something that’s not a truck. We will then add constraints to our Truck model, to make sure it can validate incoming data, and throw appropriate errors.

Goals: Expand on tests by adding edge cases, and teach the team on how to work with tests that could throw errors.

Part II - Chapter 1 - RESTful Service

Set-Up RESTful Service with Express

Length: <10 minutes We will install RESTful framework express and will remind developers on how to make basic checks to make sure we are installing safe modules, and try to document the process in a short checklist that will go into README.md file.

Goal: Start with something we already did in the last course, to remind people of our progress, and start engraining good practices.

Health Check

Length: <10 minutes Before we start working on the business functionality, we need to make sure that our services comply with Availability and Testability requirements, by exposing a GET /health endpoint.

Goal:Continue with best-practices, and make sure that our installation of the express works, and functioning as expected in basic examples.

Primer on RESTful APIs

Length: <15 minutes We are going to follow a RESTful approach to the APIs, which will ensure that in future we can convert our shop into microservices. We will start with explaining how RESTful works, and basic concepts of what is Resource, Noun and a Verb, and how RESTful APIs map to the database design.

Goal: Get team some theory on how to write RESTful APIs. We will not explore all the bonuses, namely automatic documentation tools, and test tools, but will instead show it in later stages.

Create Ice Cream

Length: <10 minutes Let team design first end-point, and make decisions on how to name, and which HTTP methods to use to create ice cream. Later on, we will review and ask questions, to guide the team towards the right decision.

Goal: Let team code, and make mistakes! And obviously, implement first Ice Cream.

Get List of Ordered Ice-Creams

  • Length: <20 minutes.* Similar to the previous step, now that we’ve created an Ice-Cream let’s return a list of previously created Ice-Creams, and create a database mock.

Goal: Empower the team to create an endpoint on their own, and learn from mistakes.

Swagger

  • Length: <30 minutes.* Let’s add Swagger if something is not documented - it doesn’t exist and Swagger is an easier way to create interactive documentation and playground for APIs. We will finish off by documenting our Ice Cream Resource.

Goal: If it’s not documented it doesn’t exist. Show the team power of RESTful APIs, and how easy it is to create documentation with a tool like Swagger.

Part II - Chapter 2 - Event Loop

Working with File System

Length: < 10 minutes Let’s remind ourselves how do Trucks look like, by generating a few good and bad trucks, and then serializing them into JSON, and finally saving files of good and bad trucks and saving them as assets to our project.

Goal: Let team shuffle in, and refresh their memory on how do trucks look like.

Saving JSON

Length: <10 minutes. Let’s use async/await, and Promise APIs to save files on the system.

Goal: Promises and Async/Await are two different interfaces to the same underlying way of working with async data. We will introduce the team by showing a simple example of how to work with both of them.

Saving and Verifying Truck JSON

Length: <35 minutes Saving files automatically is not safe, let’s automate this process, by using Node.js filesystem to create a file, writing to file, and checking the integrity of the file.

Goal: We will add complexity, in real life examples async operations rarely come alone, and instead we need to make them work together. We will explore ways to test async operations, and stub them.

Truck Endpoint

Length: <15 minutes. Let’s make sure we didn’t forget how to create RESTful endpoints, by creating a new one for the truck, and going through the motions of creating interactive documentation with Swagger.

David's User Picture