Unit testing is important because it checks small parts of code to make sure they work right and finds bugs early. It's important to do these tests before releasing an app. This guide will cover unit testing with Mocha and Chai.
Mocha is a feature-rich JavaScript test framework that runs on Node.js, making asynchronous testing simple and enjoyable. It provides functions that execute in a specific order, collecting test results and offering accurate reporting.
Chai is a BDD/TDD assertion library that can be used with any JavaScript testing framework. It offers several interfaces, allowing developers to choose the assertion style they find most comfortable.
Readable and expressive assertions
Chai provides different assertion styles and syntax options that work well with Mocha, allowing you to choose the style that suits your needs for clarity and readability.
Support for asynchronous testing
Mocha handles asynchronous testing easily, letting you test asynchronous code in Node.js applications without needing extra libraries or complex setups.
First, let's set up a new Node.js project and install the necessary dependencies:
mkdir auth-api-testing cd auth-api-testing npm init -y # Install production dependencies npm install express jsonwebtoken mongoose bcryptjs dotenv # Install development dependencies npm install --save-dev mocha chai chai-http supertest nyc
Add the following scripts to your package.json:
{ "scripts": { "test": "NODE_ENV=test mocha --timeout 10000 --exit", "test:coverage": "nyc npm test" } }
Before diving into testing, let's understand the application we'll be testing. We're building a simple but secure authentication API with the following features.
src/ ├── models/ │ └── user.model.js # User schema and model ├── routes/ │ ├── auth.routes.js # Authentication routes │ └── user.routes.js # User management routes ├── middleware/ │ ├── auth.middleware.js # JWT verification middleware │ └── validate.js # Request validation middleware ├── controllers/ │ ├── auth.controller.js # Authentication logic │ └── user.controller.js # User management logic └── app.js # Express application setup
POST /api/auth/register - Registers new user - Accepts: { email, password, name } - Returns: { token, user } POST /api/auth/login - Authenticates existing user - Accepts: { email, password } - Returns: { token, user }
GET /api/users/profile - Gets current user profile - Requires: JWT Authentication - Returns: User object PUT /api/users/profile - Updates user profile - Requires: JWT Authentication - Accepts: { name, email } - Returns: Updated user object
Create a .env.test file for test-specific configuration:
PORT=3001 MONGODB_URI=mongodb://localhost:27017/auth-api-test JWT_SECRET=your-test-secret-key
Let's create our first test file test/auth.test.js
const chai = require('chai'); const chaiHttp = require('chai-http'); const app = require('../src/app'); const User = require('../src/models/user.model'); chai.use(chaiHttp); const expect = chai.expect; describe('Auth API Tests', () => { // Runs before all tests before(async () => { await User.deleteMany({}); }); // Runs after each test afterEach(async () => { await User.deleteMany({}); }); // Test suites will go here });
Mocha provides several hooks for test setup and cleanup:
before(): Runs once before all tests
after(): Runs once after all tests
beforeEach(): Runs before each test
afterEach(): Runs after each test
Tests are organized using describe() blocks for grouping related tests and it() blocks for individual test cases:
mkdir auth-api-testing cd auth-api-testing npm init -y # Install production dependencies npm install express jsonwebtoken mongoose bcryptjs dotenv # Install development dependencies npm install --save-dev mocha chai chai-http supertest nyc
{ "scripts": { "test": "NODE_ENV=test mocha --timeout 10000 --exit", "test:coverage": "nyc npm test" } }
src/ ├── models/ │ └── user.model.js # User schema and model ├── routes/ │ ├── auth.routes.js # Authentication routes │ └── user.routes.js # User management routes ├── middleware/ │ ├── auth.middleware.js # JWT verification middleware │ └── validate.js # Request validation middleware ├── controllers/ │ ├── auth.controller.js # Authentication logic │ └── user.controller.js # User management logic └── app.js # Express application setup
POST /api/auth/register - Registers new user - Accepts: { email, password, name } - Returns: { token, user } POST /api/auth/login - Authenticates existing user - Accepts: { email, password } - Returns: { token, user }
GET /api/users/profile - Gets current user profile - Requires: JWT Authentication - Returns: User object PUT /api/users/profile - Updates user profile - Requires: JWT Authentication - Accepts: { name, email } - Returns: Updated user object
Each test should stand alone and not depend on other tests
Tests should be able to run in any sequence
Use the before, after, beforeEach, and afterEach hooks for proper setup and cleanup
PORT=3001 MONGODB_URI=mongodb://localhost:27017/auth-api-test JWT_SECRET=your-test-secret-key
const chai = require('chai'); const chaiHttp = require('chai-http'); const app = require('../src/app'); const User = require('../src/models/user.model'); chai.use(chaiHttp); const expect = chai.expect; describe('Auth API Tests', () => { // Runs before all tests before(async () => { await User.deleteMany({}); }); // Runs after each test afterEach(async () => { await User.deleteMany({}); }); // Test suites will go here });
Test boundary conditions, error scenarios, invalid inputs, and empty or null values.
describe('Auth API Tests', () => { describe('POST /api/auth/register', () => { it('should register a new user successfully', async () => { // Test implementation }); it('should return error when email already exists', async () => { // Test implementation }); }); });
Test names should clearly describe the scenario being tested, follow a consistent naming convention, and include the expected behavior.
describe('POST /api/auth/register', () => { it('should register a new user successfully', async () => { const res = await chai .request(app) .post('/api/auth/register') .send({ email: 'test@example.com', password: 'Password123!', name: 'Test User' }); expect(res).to.have.status(201); expect(res.body).to.have.property('token'); expect(res.body).to.have.property('user'); expect(res.body.user).to.have.property('email', 'test@example.com'); }); it('should return 400 when email already exists', async () => { // First create a user await chai .request(app) .post('/api/auth/register') .send({ email: 'test@example.com', password: 'Password123!', name: 'Test User' }); // Try to create another user with same email const res = await chai .request(app) .post('/api/auth/register') .send({ email: 'test@example.com', password: 'Password123!', name: 'Test User 2' }); expect(res).to.have.status(400); expect(res.body).to.have.property('error'); }); });
describe('Protected Routes', () => { let token; let userId; beforeEach(async () => { // Create a test user and get token const res = await chai .request(app) .post('/api/auth/register') .send({ email: 'test@example.com', password: 'Password123!', name: 'Test User' }); token = res.body.token; userId = res.body.user._id; }); it('should get user profile with valid token', async () => { const res = await chai .request(app) .get('/api/users/profile') .set('Authorization', `Bearer ${token}`); expect(res).to.have.status(200); expect(res.body).to.have.property('email', 'test@example.com'); }); it('should return 401 with invalid token', async () => { const res = await chai .request(app) .get('/api/users/profile') .set('Authorization', 'Bearer invalid-token'); expect(res).to.have.status(401); }); });
const mongoose = require('mongoose'); before(async () => { await mongoose.connect(process.env.MONGODB_URI_TEST); }); after(async () => { await mongoose.connection.dropDatabase(); await mongoose.connection.close(); });
describe('User CRUD Operations', () => { it('should update user profile', async () => { const res = await chai .request(app) .put(`/api/users/${userId}`) .set('Authorization', `Bearer ${token}`) .send({ name: 'Updated Name' }); expect(res).to.have.status(200); expect(res.body).to.have.property('name', 'Updated Name'); }); it('should delete user account', async () => { const res = await chai .request(app) .delete(`/api/users/${userId}`) .set('Authorization', `Bearer ${token}`); expect(res).to.have.status(200); // Verify user is deleted const user = await User.findById(userId); expect(user).to.be.null; }); });
Writing manual test cases for mocha with chai, though effective, often has several challenges:
Time-consuming: Making detailed test suites by hand can take a lot of time, especially for large codebases.
Difficult to maintain: As your application changes, updating and maintaining manual tests becomes more complex and prone to errors.
Inconsistent coverage: Developers might focus on the main paths, missing edge cases or error scenarios that could cause bugs in production.
Skill-dependent: The quality and effectiveness of manual tests depend heavily on the developer's testing skills and familiarity with the codebase.
Repetitive and tedious: Writing similar test structures for multiple components or functions can be boring, possibly leading to less attention to detail.
Delayed feedback: The time needed to write manual tests can slow down development, delaying important feedback on code quality and functionality.
To tackle these issues, Keploy has introduced a ut-gen that uses AI to automate and improve the testing process, which is the first implementation of the Meta LLM research paper that understands code semantics and creates meaningful unit tests.
It aims to automate unit test generation (UTG) by quickly producing thorough unit tests. This reduces the need for repetitive manual work, improves edge cases by expanding the tests to cover more complex scenarios often missed manually, and increases test coverage to ensure complete coverage as the codebase grows.
%[https://marketplace.visualstudio.com/items?itemName=Keploy.keployio]
Automate unit test generation (UTG): Quickly generate comprehensive unit tests and reduce redundant manual effort.
Improve edge cases: Extend and improve the scope of tests to cover more complex scenarios that are often missed manually.
Boost test coverage: As the codebase grows, ensuring exhaustive coverage becomes feasible.
In conclusion, mastering Node.js backend testing with Mocha and Chai is important for developers who want their applications to be reliable and strong. By using Mocha's testing framework and Chai's clear assertion library, developers can create detailed test suites that cover many parts of their code, from simple unit tests to complex async operations. Following best practices like keeping tests focused, using clear names, and handling promises correctly can greatly improve your testing process. By using these tools and techniques in your development workflow, you can find bugs early, improve code quality, and deliver more secure and efficient applications.
While both are testing tools, they serve different purposes. Mocha is a testing framework that provides the structure for organizing and running tests (using describe() and it() blocks). Chai is an assertion library that provides functions for verifying results (like expect(), should, and assert). While you can use Mocha without Chai, using them together gives you a more complete and expressive testing solution.
Mocha provides several lifecycle hooks for managing test data:
before(): Run once before all tests
beforeEach(): Run before each test
afterEach(): Run after each test
after(): Run once after all tests
Example:
mkdir auth-api-testing cd auth-api-testing npm init -y # Install production dependencies npm install express jsonwebtoken mongoose bcryptjs dotenv # Install development dependencies npm install --save-dev mocha chai chai-http supertest nyc
Group related tests using describe() blocks
Use descriptive test names that clearly state the expected behavior
Follow the AAA (Arrange-Act-Assert) pattern in each test
Keep tests atomic and independent
Organize test files to mirror your source code structure
Example:
{ "scripts": { "test": "NODE_ENV=test mocha --timeout 10000 --exit", "test:coverage": "nyc npm test" } }
'before' runs once before all tests, 'beforeEach' runs before each individual test, 'afterEach' runs after each individual test, and 'after' runs once after all tests are complete. These are useful for setting up and cleaning up test data.
You can use async/await or return promises in your tests. Just add 'async' before your test function and use 'await' when calling async operations. Make sure to set appropriate timeouts for longer operations.
The above is the detailed content of Unit testing for NodeJS using Mocha and Chai. For more information, please follow other related articles on the PHP Chinese website!