Unit Testing Tool ‐ Mocha - JUCSE49-Mavericks/Smart-Class-Routine-Management-System GitHub Wiki
Mocha: A JavaScript Test Framework
Author
: Akila Nipo
Overview
Mocha is a widely-used JavaScript test framework that runs on Node.js and in the browser. It allows developers to write and execute tests for their applications efficiently, offering a flexible and extensible environment that supports various testing styles and methodologies.
Key Features of Mocha
➡️ For more, explore Mocha's features here
Mocha Vs Other Unit Testing Tools
Mocha vs. Jest
📌Feature | Mocha | Jest |
---|---|---|
Flexibility & Customization | Modular design allows using various custom libraries (Chai, Sinon) for assertions and mocking, providing greater flexibility. | More opinionated, with built-in tools for assertions, mocking, etc. |
Integration with Other Tools | Easily integrates with a wide range of third-party tools and libraries, making it ideal for diverse setups. | Less flexible; optimized for its own ecosystem and tools. |
Large Projects (Test Coverage) | Greater configurability for handling large and complex projects, allowing tailored test setups. | Simpler, but might lack granular control for large-scale test setups. |
Parallelism & Process Control | Fine-grained control over test execution order and environment setup, making it suitable for complex workflows. | Runs tests in parallel by default; less flexible for customized test execution. |
Legacy Projects & Ecosystem | Better suited for legacy projects with older or non-standard codebases, ensuring compatibility. | More modern and suited for newer projects, but less adaptable to legacy setups. |
Browser Testing | Easily supports testing in real browser environments, allowing for comprehensive integration tests. | Typically focused on Node.js, with browser simulation via JSDOM. |
Reporters & Output Customization | Offers a large variety of customizable reporters for test results, providing detailed feedback. | Fewer built-in options for customizing test output. |
Non-Node.js Environments | Works well in both Node.js and browser-based environments, making it versatile for various applications. | Optimized for Node.js environments, less flexible for non-Node.js contexts. |
Mocha vs. Cypress
📌Feature | Mocha | Cypress |
---|---|---|
Focus | Primarily for unit and integration testing, offering flexibility for various test types. | Focused on end-to-end testing for web applications. |
Browser Testing | Can run tests in both Node.js and browsers, offering more flexibility. | Runs tests directly in real browsers, simulating user interactions. |
Test Execution Speed | Dependent on test runner and browser setup. | Faster for end-to-end tests as it directly interacts with browsers. |
Debugging | Relies on external tools for debugging (e.g., DevTools). | Built-in time travel and snapshots for easy debugging. |
Automatic Waiting | Requires manual wait and timeout handling. | Automatically waits for DOM elements to load, reducing test flakiness. |
Real-Time Reloading | Not available natively; requires configuration. | Built-in real-time reloads when saving files, making development faster. |
Assertion Libraries | More flexible, supports custom assertion libraries like Chai or Should. | Has its own built-in assertions, but supports custom libraries as well. |
CI/CD Integration | Integrates with various CI tools, offering greater flexibility through plugins. | Seamless integration with popular CI/CD tools like Jenkins, CircleCI, etc. |
Mocha vs. Jasmine
📌Feature | Mocha | Jasmine |
---|---|---|
Flexibility | More flexible, allowing different assertion libraries (Chai, Should, etc.). | More opinionated with built-in assertions and spies. |
Setup | Simpler to set up and configure, especially for custom setups. | Has more built-in features, which can add complexity to setup. |
Test Doubles (Mocks/Spies) | Relies on external libraries like Sinon for |
Setting Up Mocha : A Quick Guide
📋 1️⃣ Prerequisites
- Ensure Node.js is installed
2️⃣ Install Mocha
-
Local installation
npm install mocha
-
Global installation
npm install -g mocha
-
Install as a development dependency
npm install --save-dev mocha
package.json
3️⃣ Add a Test Script in Open your package.json
file and add the following under "scripts"
:
"scripts": {
"test": "mocha"
}
4️⃣ Create a Test Directory and Files
I've created a folder named test/
in the root of my project.
Inside the test/
folder, I have created a test file (e.g., syllabusFilter.test.js
).
5️⃣ Write a Test for Sample Test Case
In this step,I've written test cases for the fetchCourseData
function from my syllabusFilterController.js
. Here’s how you to set it up:
🔹 5.i) fetchCourseData
method:
🔹 5.ii) Import necessary libraries in test/syllabusFilterController.js
🔹 5.iii) should return an error if department not found: This test ensures that the fetchCourseData
function correctly returns an error with the message 'Department not found' when an invalid department name is provided.
🔹 5.iv) should return an error if session not found: This test verifies that the fetchCourseData
function returns the appropriate error message 'Session not found' when a valid department is given but an invalid session is requested.
🔹 5.v) should return an error if the exam year does not exist: This test checks that the fetchCourseData
function responds with the error message 'Exam year not found' when valid department and session parameters are supplied, but the specified exam year does not exist.
6️⃣ Run Tests
Execute the following command to run your tests:
npx mocha .\syllabusFilter.test.js