Write Good Tests With Mocha
Learn to use the Mocha framework and the Node.js assert library to write, automate, and organize tests in Javascript.
StartKey Concepts
Review core concepts you need to learn to master this subject
before()
Hooks
beforeEach()
Hooks
after()
Hooks
afterEach()
Hooks
Test Frameworks
describe()
functions
it()
functions
The assert
Library
before()
Hooks
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
- 1Testing 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…
- 2Before 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…
- 3After 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…
- 4In 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…
- 6In 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…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory