Skip to Content
Learn
Automate and Organize Tests
Install Mocha I

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 allows you to download packages from the web, and manage them in a JavaScript project
  • Mocha is one of those packages and is used to test other JavaScript code

A JavaScript project is a directory of files. The following command creates a file package.json that can be used to manage packages for the project.

$ npm init

After running this command you will be prompted to enter information about your project. It’s okay to skip some fields if you’re not ready to enter that information.

With your project setup, you can install packages.

$ npm install mocha -D

Here’s what this command means:

  • npm install tells npm to install a package from the internet and any other packages it depends on
  • mocha is the package you want to download
  • -D notes that this package is a dependency for your project, which makes it easier for other developers to use

Once you npm install packages, you can find the packages and all their dependencies in the node_modules folder. The new directory structure contains the following:

project |_ node_modules |___ .bin |___ mocha |___ ... |_ package.json

The ... in the file structure represents other packages that are a dependency for Mocha.

Instructions

1.

Initialize the project. In the terminal window type:

$ npm init

Hit the Enter key to skip each prompt. This creates a package.json file in your project directory.

Check your work after each instruction.

2.

Install Mocha as a package and save it as a dependency. In the terminal window type:

$ npm install mocha -D

You can view package.json in the text editor. You can now see mocha as a dependency!

Folder Icon

Sign up to start coding

Already have an account?