Key Concepts

Review core concepts you need to learn to master this subject

before() Hooks

before(() => { path = './message.txt'; });

In a test file, the function before() will be executed first, regardless of it’s placement in the code block. before() is often used to set up code, like variables and values, for other function calls to use in their execution.

Automate and Organize Tests
Lesson 1 of 2
  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
    So far, we’ve been writing just one test using a single it() block. However, in most situations, we will need to write many tests for a particular feature that get executed in succession. Running…
  8. 8
    Over the last two exercises, we learned about the four main phases of a test: setup, execute, verify, and teardown. In the last exercise, you may have noticed that the setup and teardown steps were…
  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…

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