Skip to content

StyvenSoft/JavaScript-Unit-Testing

Repository files navigation

JavaScript Unit Testing

Learning to write unit tests in JavaScript with Mocha.

  • Define an automated test suite

  • Describe how a test suite is used in software development

  • Explain the benefits of automated testing

Why Test?

LESSON 1

Learn the benefits of software testing and the terminology to explain them.

Exercises

  1. Introduction

  2. Manual Testing

  3. Automated Testing

The workflow might look like this:

  • Write code and corresponding tests
  • Enter a command into a terminal to run tests
  • If the app behaves as intended, all tests should pass. Development is complete.
  • If it does not behave as intended, at least one test should fail. Fix code and return to step 2.
  1. The Test Suite

  2. Tests As Documentation

  3. Regression

Automate and Organize Tests

LESSON 2

Use the Mocha framework to automate and organize tests.

Exercises

  1. Introduction

Testing is an essential part of development. When used properly, testing can catch and identify issues with your implementation code before you deploy it to users. Instead of testing every function manually, developers automate their tests with a test framework.

  1. Install Mocha I
  • The following command creates a file package.json that can be used to manage packages for the project.
$ npm init

With your project setup, you can install packages.

$ npm install mocha -D

The new directory structure contains the following:

project
|_ node_modules
|___ .bin
|___ mocha
|___ ...
|_ package.json
  1. Install Mocha II

The first (and more tedious) method is to call it directly from node_modules:

$ ./node_modules/mocha/bin/mocha

set the value of "test" to mocha. It should look like this:

"scripts": {
  "test": "mocha"
}

Now you can call Mocha with the following command:

$ npm test
  1. describe and it blocks

  2. assert

  3. Setup, Exercise, and Verify

  4. Teardown

  5. Hooks

Write Expressive Tests

LESSON 3

Use the Node.js assert library to write more expressive tests.

Exercises

  1. Introduction

A good test framework is fast, complete, reliable, isolated, maintainable, and expressive. In this lesson you will learn how to use Node’s assert library to write more expressive tests.

  1. assert.ok
  • As a Node module, assert can be imported at the top of your files with
const assert = require('assert');
  1. assert.equal

  2. assert.strictEqual

  3. assert.deepEqual I

  4. assert.deepEqual II

  5. Other assert methods

Read the documentation for assert.notEqual()

Learn TDD With Mocha

LESSON 4

Practice test-driven development to create a JavaScript testing suite using Mocha.js.

Exercises

  1. Introduction
  • Test-driven development (TDD) is a programming technique where you write test code before implementation code. Test code is written to define the desired behavior of your program.

red-green-refactor-tdd

  1. Getting Into The Red I

  2. Red To Green I

  3. Refactor I

  4. Getting into the Red II

  5. Red to Green II

  • Write a for loop that iterates inputArrayLength number of times and adds the value of each element to totalSum.
 const Calculate = {
	sum(inputArray) {
    
    let totalSum = 0;
    const inputArrayLength = inputArray.length;
		for (let i = 0; i < inputArrayLength; i++){
      totalSum += inputArray[i]
    }
    return totalSum
	}
}

module.exports = Calculate;
  1. Refactor II
  • Refactor the code in your index.js file by replacing the current implementation code inside Calculate.sum() with a function that uses the built-in JavaScript method .reduce() to accumulate the total value of an array of numbers.
const Calculate = {
  sum(inputArray) {

    return inputArray.reduce((sum, value) => {
      return sum + value;
      if (inputArray.length === 0) {
        return 0;
      }
    })
  }
}

module.exports = Calculate;
  1. Edge Case

TDD Feature-Level Tests

LESSON 5

Learn the process of writing feature level tests for a full stack web app, using Outside-in TDD.

Exercises

  1. Introduction

Often the hardest part of creating a full-stack web application is knowing where to start. In this lesson, we will use an outside-in development process. With this approach, we start to build our new functionality at the feature level.

  • You will learn a few tools for writing feature-level tests.

  • You will go through a few rounds of the TDD cycle at the feature level to build an application that renders user input.

  • You will end the lesson “in the red,” with a failing feature-level test that you can only address by “dropping” to the server level.

  1. Feature Test Toolbelt

Chai

Node.js has a default assertion library that provides enough functionality to write basic test code. The Chai testing library extends the types of assertions we can make.

const {assert} = require('chai');
  1. Feature Test I
const {assert} = require('chai');

describe('User visits root', () => {
  describe('without existing messages', () => {
    it('starts blank', () => {
      browser.url('/');
    })
  });
});
  1. Feature Test I: Assert
const {assert} = require('chai');

describe('User visits root', () => {

  describe('without existing messages', () => {
    it('starts blank', () => {
      browser.url('/');

      assert.equal(browser.getText('#messages'), '');
    });
  });
});
  1. Feature Test I: Passing, Setup, Exercise, Verify

  2. Stuck In The Red