Learn

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 signifies that this package is a development dependency and will show up under the devDependecies section in package.json. This means that the package will only be included in development mode and will not be included in the production bundle.

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!

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?