DEV Community

Abhishek Gupta
Abhishek Gupta

Posted on

Backend Testing โ€” Complete Notes

๐Ÿ“Œ 1. What is Backend Testing?

Definition:

Backend testing means checking whether your server-side logic works correctly in all situations.


Simple Understanding:

When a user sends request:

User โ†’ API โ†’ Backend โ†’ Database โ†’ Response
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ You must ensure:

  • Correct response is returned
  • Errors are handled properly
  • Data is stored correctly

โ— 2. Why Backend Testing is Important

Without Testing:

  • Bugs go to production โŒ
  • Users face crashes โŒ
  • Data corruption โŒ
  • Security issues โŒ

With Testing:

  • Bugs caught early โœ…
  • Safe deployment โœ…
  • Confident coding โœ…
  • Faster development โœ…

Real Example:

Case: User signup API
Enter fullscreen mode Exit fullscreen mode

Without testing:

  • Duplicate emails allowed โŒ
  • Password not validated โŒ

With testing:

  • Duplicate โ†’ rejected โœ…
  • Invalid input โ†’ error โœ…

๐Ÿง  3. What Should You Test?

You should test:

โœ… Functional Behavior

  • API works correctly

โœ… Validation

  • Wrong input handled

โœ… Security

  • Unauthorized access blocked

โœ… Database

  • Data saved correctly

โœ… Errors

  • Server errors handled

๐Ÿงฉ 4. Types of Backend Testing


๐Ÿ”น 1. Unit Testing

Definition:

Test single function in isolation


Example:

function add(a, b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Test:

expect(add(2, 3)).toBe(5);
Enter fullscreen mode Exit fullscreen mode

Features:

  • Fast โšก
  • No DB
  • No API

๐Ÿ”น 2. Integration Testing (MOST IMPORTANT)

Definition:

Test multiple layers together

Route โ†’ Controller โ†’ Service โ†’ DB
Enter fullscreen mode Exit fullscreen mode

Example:

POST /users
Enter fullscreen mode Exit fullscreen mode

Test:

  • Data sent
  • Saved in DB
  • Response returned

๐Ÿ”น 3. End-to-End Testing (E2E)

Definition:

Test full user journey


Example:

Register โ†’ Login โ†’ Access profile
Enter fullscreen mode Exit fullscreen mode

Features:

  • Real-world testing
  • Slower

๐Ÿ—๏ธ 5. Backend Testing Tools

๐Ÿฅ‡ Core Tools

  • Jest
  • Supertest
  • MongoDB Memory Server
  • dotenv

๐Ÿง  6. Role of Each Tool

๐Ÿงช Jest

  • Runs tests
  • Provides describe, it, expect
  • Handles assertions
  • Supports mocking

๐ŸŒ Supertest

  • Sends HTTP requests
  • Tests APIs like Postman

๐Ÿ—„๏ธ MongoMemoryServer

  • Fake DB
  • Fast & isolated

โš™๏ธ dotenv

  • Separate test environment

โš™๏ธ 7. How Testing Works (Flow)

Jest starts test
 โ†“
Supertest sends request
 โ†“
Backend processes request
 โ†“
DB stores data
 โ†“
Response returned
 โ†“
Jest verifies result
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฑ 8. Basic Setup

Install:

npm install --save-dev jest supertest mongodb-memory-server dotenv
Enter fullscreen mode Exit fullscreen mode

package.json

"scripts": {
  "test": "jest"
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿงช 9. First Test Example

import request from 'supertest';
import app from '../app';

describe('GET /', () => {
  it('should return message', async () => {
    const res = await request(app).get('/');

    expect(res.statusCode).toBe(200);
    expect(res.body.message).toBe('API working');
  });
});
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ฅ 10. Real Backend Test Cases

โœ… Success Case

expect(res.statusCode).toBe(200);
Enter fullscreen mode Exit fullscreen mode

โŒ Validation Error

expect(res.statusCode).toBe(400);
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”’ Unauthorized

expect(res.statusCode).toBe(401);
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Duplicate Data

expect(res.body.message).toMatch(/already exists/i);
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฅ Server Error

expect(res.statusCode).toBe(500);
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” 11. Authentication Testing

let token;

beforeAll(async () => {
  const res = await request(app)
    .post('/login')
    .send({ email, password });

  token = res.body.token;
});

it('should access protected route', async () => {
  const res = await request(app)
    .get('/profile')
    .set('Authorization', `Bearer ${token}`);

  expect(res.statusCode).toBe(200);
});
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  12. Database Testing

Use:

  • MongoDB Memory Server

Why:

  • No real DB
  • Fast
  • Clean

๐Ÿงช 13. Mocking

Why needed?

To avoid calling real services


Example:

jest.mock('../emailService', () => ({
  sendEmail: jest.fn(),
}));
Enter fullscreen mode Exit fullscreen mode

โšก 14. Best Practices

โœ… Do:

  • Test real API behavior
  • Cover edge cases
  • Keep tests independent

โŒ Donโ€™t:

  • Use real DB
  • Start server
  • Ignore errors

๐Ÿง  15. Testing Pyramid

Unit โ†’ Most
Integration โ†’ Medium
E2E โ†’ Few
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“Š 16. Code Coverage

npm test -- --coverage
Enter fullscreen mode Exit fullscreen mode

โš™๏ธ 17. CI/CD Testing

Use:

  • GitHub Actions

๐Ÿง  18. Interview Questions

Q: Why Jest?

๐Ÿ‘‰ Easy + powerful


Q: Why Supertest?

๐Ÿ‘‰ API testing without server


Q: Unit vs Integration?

๐Ÿ‘‰ Function vs full flow


๐Ÿš€ 19. Advanced Topics

  • TDD
  • Load testing
  • Security testing
  • Contract testing

๐Ÿ 20. Final Summary

Jest = test runner
Supertest = API tester
DB memory = safe DB
Enter fullscreen mode Exit fullscreen mode

๐Ÿง  Final Thought

Testing is not about checking code
It is about preventing failure before users face it

Top comments (0)