Intro to Test Specs

As a developer, testing your code before it reaches any sort of live environment is critical to ensuring that problems don't occur for end users.

A test spec (specification) is a test that you can run against code to see if the code meets the specifications. As a student, you will find test specs very useful for gauging if you are on the right track to solving a given problem statement. Test specs will eventually be used to grade assessments.

For now, you will not have to write your own tests, but you will have to run the tests written for you.

In this reading, you will learn how to:

Testing Framework - Mocha

In JavaScript, one of the most popular unit testing libraries is Mocha. All the test specs that you will be running in the first half of App Academy will use Mocha.

Tests in Mocha assert that a certain condition is met when running code. Test specs will help you determine if you have achieved the minimum requirements for the code to be passable. At App Academy, test specs will be used to assess if you have completed the requirements of a coding problem for assignments and assessments.

Running Tests

Running a Mocha test is very straightforward. In problem sets provided to you, the test files will be located in the test directory.

After downloading an App Academy project, navigate to the project's root directory. This is typically where you will find the test directory that contains the test specs for the project.

For example, the file structure for a testing-demo project may look like this:

testing-demo/
├──  problems/
│     ├── problem-one.js
│     └── problem-two.js
├──  test/
│     ├── problem-one-spec.js
│     └── problem-two-spec.js
├──  package-lock.json
├──  package.json
└──  README.md
    

Here's how to run the test specs using this example:

  1. First, navigate into the project directory--here, testing-demo--in your terminal.
  2. Then, run the setup command in your terminal to install Mocha and other dependencies:
$ npm install
    

Any time you want to test your code by running the test specs, run this command in your terminal:

$ npm test
    

After running npm test, the results of the tests should be printed to your terminal, detailing which tests ran, which tests passed (denoted by checkmarks), how many tests passed, and how long the tests took to run.

Here's an example of the output when all test specs pass:

  Problem One
    ✔ should <some assertion>
    ✔ should <some other assertion>

  2 passing (5ms)
    

Here's an example of the output when one test spec fails and the other one passes:

  Problem One
    1) should <some assertion>
    ✔ should <some other assertion>

  1 passing (5ms)
  1 failing

  1) Problem One
       should <some assertion>

       AssertionError [ERR_ASSERTION]: false == true
      + expected - actual

      -false
      +true
    

Notice that when you fail a test spec, Mocha prints an error message that corresponds to the failed test spec. You can use this error message as a clue to figure out how to make the test spec pass!

You can also run tests from only a single file by specifying the file's path and name on the command line. For instance, to run only the test specs for problem-one.js in the scenario above, you would run the following command:

$ npm test test/problem-one-spec.js
    

What you've learned

In this reading, you learned what Mocha tests are, why they are important, how to run them, and how to read the test output.