Testing

Testing Frameworks

Mocha

Assertion libraries are used to actually write tests. Node has a library called Chai and Assert

 // first npm install chai and chai-spies
const chai = require("chai");
const expect = chai.expect;
const spies = require("chai-spies");
chai.use(spies);

Importing assert module:
const assert = require(“assert")

describe()

it()

context

The Testing Pyramid

Test-Driven Development (TDD)

  1. Writing tests before code ensures that the code written works.
    • Code written to pass specs is guaranteed to be testable.
    • Code with pre-written tests easily allows other developers to add and test new code while ensuring nothing else breaks along the way.
  2. Only required code is written.
    • In the face of having to write tests for every piece of added functionality TDD can help reduce bloated un-needed functionality.
    • TDD and YAGNI ("you ain't gonna need it") go hand in hand!
  3. TDD helps enforce code modularity.
    • A TDD developer is forced to think about their application in small, testable chunks - this ensures the developer will write each chunk to be modular and capable of individual testing.
  4. Better understanding of what the code should be doing.
    • Writing tests for a piece of code ensures that the developer writing that code knows what the piece of code is trying to achieve.

TDD Workflow