Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary Concepts #1047

Open
oldoc63 opened this issue Jun 26, 2022 · 2 comments
Open

Preliminary Concepts #1047

oldoc63 opened this issue Jun 26, 2022 · 2 comments
Assignees

Comments

@oldoc63
Copy link
Owner

oldoc63 commented Jun 26, 2022

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 manually, developers automate their tests with a test framework.
Developers use test frameworks to organize and automate tests that provide useful feedback when errors occur. We will use the Mocha test framework to write tests against JavaScript methods.
We'll learn:

  • To write a basic Mocha test suite
  • Use Node's assert.ok method to verify the expected output of your code
  • Understand and apply the four phases of a test to create an expressive testing suite
  • Evaluate the quality of your tests against the characteristics of a good test
@oldoc63 oldoc63 self-assigned this Jun 26, 2022
@oldoc63
Copy link
Owner Author

oldoc63 commented Jun 26, 2022

Install Mocha

Before writing any test 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.
Initialize the project:

npm init

Install Mocha:

npm install mocha -D

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

@oldoc63
Copy link
Owner Author

oldoc63 commented Jun 27, 2022

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 (and recommended) method is to add a script to package.json. In the scripts object in package.json, set the value of "test": "mocha". It should look like this:

"scripts": {
"test": "mocha"
}

Now you can call Mocha with the following command:

npm test

Instead of manually running each test in the test directory, you can use this command to run the full test suite automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant