Skip to content

Latest commit

 

History

History
90 lines (61 loc) · 3.15 KB

module-8.1-unit-testing-with-mocha-and-chai.md

File metadata and controls

90 lines (61 loc) · 3.15 KB

8.1. Unit Testing with Mocha and Chai

Unit testing is a fundamental practice in software development to ensure that individual components of your codebase work correctly in isolation. In this section, you will learn about unit testing using Mocha and Chai, two widely used JavaScript testing frameworks. We'll cover the basics and provide examples to help you get started.

What is Mocha?

Mocha is a flexible and feature-rich JavaScript test framework that provides a comprehensive testing suite. It supports various assertion libraries, including Chai, making it an excellent choice for unit testing.

What is Chai?

Chai is a popular assertion library for JavaScript. It works well with Mocha and allows you to write expressive and human-readable assertions about the behavior of your code.

Setting Up Mocha and Chai

Before you start writing tests, you need to set up Mocha and Chai in your project. You can do this using Node.js and npm (Node Package Manager). Here's how to set up a basic project structure:

  1. Create a new directory for your project and navigate to it in the terminal.

  2. Initialize a new Node.js project by running:

    npm init -y
  3. Install Mocha and Chai as development dependencies:

    npm install mocha chai --save-dev
  4. Create a directory to store your tests. Let's name it test.

  5. Inside the test directory, create a JavaScript file for your tests. For example, my-test.js.

Writing Your First Test

Now that your project is set up, you can start writing tests. In your my-test.js file, you'll use Mocha and Chai to write a simple test case.

const chai = require('chai');
const assert = chai.assert;

// The function to be tested
function add(a, b) {
  return a + b;
}

// Describe the test suite
describe('add function', function () {
  // Test case 1
  it('should add two numbers', function () {
    const result = add(2, 3);
    // Use Chai assertions to check the result
    assert.equal(result, 5);
  });

  // Test case 2
  it('should add two negative numbers', function () {
    const result = add(-2, -3);
    // Use Chai assertions to check the result
    assert.equal(result, -5);
  });
});

In the example above, we're testing the add function to ensure it correctly adds two numbers. We describe a test suite using describe and define individual test cases with it. In each test case, we use Chai's assertions to make specific claims about the function's behavior.

Running Tests

After writing your tests, you can run them using Mocha. To do this, add a script to your package.json file:

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

Now, you can run your tests with:

npm test

Mocha will discover and run the tests in the test directory.

Conclusion

Unit testing with Mocha and Chai is a crucial practice for ensuring the correctness of your code. Writing tests helps catch bugs early and maintain the reliability of your codebase as it evolves. As you become more comfortable with Mocha and Chai, you can write comprehensive test suites to validate the behavior of your functions and modules.