Testing in JavaScript (Node.js): frameworks and tools
Quality testing is an integral part of Node.js development. It allows you to identify errors at early stages, simplifies refactoring, and increases application reliability. The JavaScript ecosystem has many frameworks and utilities for testing. In this article, we will look at the main tools, their capabilities, and provide practical examples.
1. Main types of tests
Before choosing a tool, it is important to understand what types of tests you will write:
- Unit tests — check individual functions or modules in isolation.
- Integration tests — check the interaction between several modules, a database, or external APIs.
- End-to-End (E2E) tests — simulate user actions in a browser or a full application.
2. Jest — a universal framework from Facebook
Jest is the most popular framework for testing JavaScript. It includes everything you need: a test runner, an assertion library, mocks, and built-in code coverage support.
Installation:
npm install --save-dev jestExample of a simple unit test for an addition function:
// sum.jsfunction sum(a, b) { return a + b;}module.exports = sum;
// sum.test.jsconst sum = require('./sum');
test('adds 1 + 2 equals 3', () => { expect(sum(1, 2)).toBe(3);});Running tests:
npx jestAdvantages of Jest:
- Zero configuration for React and Node projects.
- Automatic launch in watch mode.
- Built-in mocks and spies.
- Support for asynchronous code (async/await, callbacks).
3. Mocha + Chai — a flexible bundle
Mocha is a flexible test runner that does not impose an assertion library. It is most often used together with Chai — a library with a clear syntax (should, expect, assert).
Installation:
npm install --save-dev mocha chaiExample test with Chai:
// calculator.jsfunction divide(a, b) { if (b === 0) throw new Error('Division by zero'); return a / b;}module.exports = { divide };
// test.jsconst { expect } = require('chai');const { divide } = require('./calculator');
describe('Calculator', function() { it('should divide two numbers', function() { expect(divide(10, 2)).to.equal(5); });
it('should throw error when dividing by zero', function() { expect(() => divide(10, 0)).to.throw('Division by zero'); });});Running:
npx mocha test.jsMocha is especially convenient when you need fine-tuning of reports and integration with other tools.
4. Supertest — testing HTTP servers
Supertest is a library for integration testing of Express (and other) servers. It allows you to send HTTP requests and check responses.
Installation:
npm install --save-dev supertestExample test for an Express application:
// app.jsconst express = require('express');const app = express();app.get('/hello', (req, res) => res.json({ message: 'Hello World' }));module.exports = app;
// app.test.jsconst request = require('supertest');const app = require('./app');
describe('GET /hello', function() { it('responds with json', function(done) { request(app) .get('/hello') .expect('Content-Type', /json/) .expect(200) .then(response => { expect(response.body.message).toBe('Hello World'); done(); }); });});Supertest works great with Jest or Mocha.
Also in library
Java Task Scheduler: Quartz Scheduler – Complete Guide
ORM for TypeScript: comparison of TypeORM, Prisma and MikroORM
Swift Data Complete Guide: ORM for Swift with Examples
Working with ZIP Archives in PHP: A Complete Guide to ZipArchive
HTTP Client for C# (.NET): Top Libraries for Working with Web Requests