Chevron Left Icon
Automate and Organize Tests
Lesson 1 of 2
Chevron Right Icon
  1. 1
    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…
  2. 2
    Before writing any tests you’ll need to use Node.js and npm to set up a JavaScript project and install Mocha. - Node allows you to run JavaScript in the terminal - npm is a Node tool that a…
  3. 3
    After installing Mocha as a dependency we can run it in two ways. The first (and more tedious) method is to call it directly from node_modules: $ ./node_modules/mocha/bin/mocha The second (a…
  4. 4
    In Mocha we group tests using the describe function and define tests using the it function. These two functions can be used to make your test suite complete, maintainable, and expressive in t…
  5. 5
    In the last exercise, you learned how to organize and automate tests using the Mocha test framework. To write the tests themselves, we can use the assert.ok method provided by Node.js. In programm…
  6. 6
    In this exercise you will be separating a test into setup, exercise, and verify phases. This distinct and well-defined separation of steps makes your test more reliable, maintainable, a…
  7. 7
    Some tests require a fourth phase called teardown. This step makes your test isolated. - Teardown - reset any conditions that were changed during the test A test, like the example in this e…
  8. 8
    Using teardown in the it block made your test isolated, but not reliable. If the system encounters an error before it reaches the teardown, it will not execute that phase. In the previous exam…
  9. 9
    You can now write tests with Mocha and Node’s assert.ok() ! In this lesson you learned to: - Install Mocha with npm - Organize tests with describe() and it() - Ensure your tests are isolated and e…
  1. 1
    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 te…
  2. 2
    As a Node module, assert can be imported at the top of your files with const assert = require(‘assert’); The functions in the assert library compare values and throw errors as needed using one …
  3. 3
    You can use assert.ok() for most verifications, but sometimes it can be difficult to determine the condition you are evaluating. Read the example code below. Will this assertion throw an error? co…
  4. 4
    Take a look at the code below. Will these assertions throw errors? const a = 3; const b = ‘3’; assert.ok(a == b); assert.ok(a === b); - The first assertion will not throw an error because it uses…
  5. 5
    Do you think these assertions will throw errors? const a = {relation: ‘twin’, age: ‘17’}; const b = {relation: ‘twin’, age: ‘17’}; assert.equal(a, b); assert.strictEqual(a, b); Both assertions …
  6. 6
    In the last exercise you used deepEqual() to compare the values of two objects with loose equality. Arrays are also objects, so deepEqual() also compares their values with loose equality. const ar…
  7. 7
    Node’s assert library includes more than the four methods covered so far. You can find them all in the Node.js documentation . Many of the methods can be implemented using other methods, like u…
  8. 8
    You can now write tests with Node’s assert library! In this lesson you learned to: - Check for loose (==) equality with assert.equal() - Check for strict (===) equality with assert.strictEqual() - …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo