DEV Community

Ravi Bhuvan
Ravi Bhuvan

Posted on • Edited on

Tests using Jest and Supertest

  1. What is a Local Test?

A local test is a small script that runs on your computer inside your terminal (using npm test) to automatically test your code without needing a browser, Postman, or any cloud server.

Right now, if you want to check if your backend works, you might:
1. Start your server (node server.js).
2. Open Postman or your browser.
3. Send a request to http://localhost:3000/todo.
4. Manually look at the JSON response to see if it's correct.
Enter fullscreen mode Exit fullscreen mode

There are Main two Tools we use

Jest (The Test Engine & Runner)

  • Manages running your test files.
  • Provides test structure syntax like test(...) and assertions like expect(response.status).toBe(200).

Supertest (The Fake HTTP Client)

  • Acts like Postman inside your code
  • Sends fake HTTP requests (GET, POST, PUT, DELETE) directly into your Express app.
Example Code
const request = require('supertest');
const app = require('./app'); // Your Express app

describe('GET /todo', () => {
  test('should return status 200 and a list of todos', async () => {
    // 1. Supertest sends a fake GET request to /todo
    const response = await request(app).get('/todo');

    // 2. Jest checks (asserts) the result
    expect(response.statusCode).toBe(200);
    expect(Array.isArray(response.body)).toBe(true);
  });
});
Enter fullscreen mode Exit fullscreen mode

CI/CD

  • CI/CD is a software development practice that automates the process of testing, building, and delivering/deploying code.
  • two main parts:
    • Continuous Integration (CI)
    • Continuous Delivery / Deployment (CD)

CI

  • Whenever developers push or merge code, an automated pipeline checks whether the new code works correctly.
Code pushed
    ↓
Checkout code
    ↓
Install dependencies
    ↓
Build
    ↓
Run tests
    ↓
Pass / Fail
Enter fullscreen mode Exit fullscreen mode

Top comments (0)