Difficulty: beginner
Estimated Time: 15 minutes
  • Educational Objective
  • What You Need To Know Before You Start
  • Understanding Node.js Code Coverage using Istanbul/Nyc
  • Scenario Contents
  • Getting the Source Code from GitHub

IMPORTANT: You need to do the steps in sequence in order for the state of the lesson's learning environment to be consistent. Otherwise, you'll get behaviors that might be confusing.


Educational Objective

The objective of this scenario is to demonstrate how to apply Mocha unit testing and Istanbul/Nyc code coverage to an application running under Node.js.

What You Need To Know Before You Start

In order to get full benefit from the concepts and tasks presented in this scenario you need to have basic understanding of how to enter commands in the Katacoda interactive learning environment. Each step has commands that can be entered into the interactive terminal automatically by clicking on the command in the left side text pane of the interactive environment's web page.

There is no need to type directly into the terminal window. Clicking on a command will automatically execute it.

Also, it's useful to understand the basics of programming Node.js using Javascript as well as the basic of running unit tests in the Mocha test framework.

Understanding Node.js Code Coverage using Istanbul/Nyc

Code coverage is the process of measuring how many lines of code get exercised by a project's unit tests. The Node.js package used to execute unit tests on the code base for the demonstration application is Mocha. The page used to generate coverage reports based the Mocha's test results is Istanbul/Nyc.

You'll run the tests in Step 4 of this scenario.

After you run the unit test and coverage tests you'll out put that looks something similar to this:

6 passing (113ms)
  
  -----------|----------|----------|----------|----------|-------------------|
  File       |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
  -----------|----------|----------|----------|----------|-------------------|
  All files  |    90.91 |    81.25 |      100 |    92.86 |                   |
   server.js |    90.91 |    81.25 |      100 |    92.86 |          15,16,29 |
  -----------|----------|----------|----------|----------|-------------------|
  

You'll view the details of this report when you do the step.

Scenario Contents

Here's a listing of the steps you'll take in the scenario.

Step 1 - Installing the Node.js Application

Step 2 - Reviewing the Code

Step 3 - Reviewing the Tests

Step 4 - Running the Tests with Coverage and Reviewing the Results

Step 5 - Using the Pinger API

To proceed, click the Continue button.


Getting the Source Code from GitHub

You can view the source code for the pinger demonstration application and test regimine can be found on GitHub, here.

You've crossed the finish line!

In this scenario you learned:

  • How to install the pinger application that was written in node.
  • The basic operations of the pinger application
  • To review the tests to get a sense of testing scope
  • How to run tests under mocha using istanbul/nyc to do test coverage
  • How to analyse the test results by reading the coverage report generated by

Using Istanbul/Nyc to Test a Node Application

Step 1 of 5

Step 1 - Installing the Node.js Application

The first thing we need to do is get the code for the demonstation application pinger from GitHub and clone it into the Katacoda interactive learning environment.

Task 1: Click or type the following in the interactive terminal to clone the pinger code into Katacoda.

git clone https://github.com/reselbob/pinger.git

Continue on to the next step....