Testing is the process of checking software to ensure it works correctly, meets requirements, and is free of bugs.
- ✅ Finds bugs early
- ✅ Ensures software quality
- ✅ Prevents crashes and failures
- ✅ Saves time and cost
- ✅ Improves security
- ✅ Builds confidence
- ✅ Enhances user experience
- ✅ Validates requirements
- What it is: Testing individual components (like functions, methods, or classes) in isolation.
- Purpose: To ensure that each unit of code works correctly on its own.
- Example: Testing a
calculateTotal(price, tax)
function to see if it returns the correct result. - Tools: Jest, Mocha, JUnit, NUnit, PyTest, etc.
- Done by: Developers.
- What it is: Testing the interaction between multiple components or modules.
- Purpose: To verify that integrated units work together correctly.
- Example: Testing a user login flow where the frontend form, backend API, and database are all involved.
- Tools: Postman, JUnit, Supertest, TestNG, etc.
- Done by: Developers or QA engineers.
- What it is: Testing the entire application flow from the user's perspective.
- Purpose: To ensure the whole system works as expected across all layers (frontend, backend, database).
- Example: Simulating a real user who signs up, logs in, and completes a purchase.
- Tools: Cypress, Playwright, Selenium, Puppeteer.
- Done by: QA engineers or automation testers.
- jest
- moka
- Nodejs build-in
Mocking is a technique used in testing where you create fake versions of functions, modules, or services so you can:
- Isolate the unit you're testing
- Avoid calling real dependencies (e.g., databases, APIs)
- Simulate different scenarios and behaviors
- ✅ Test in isolation – Focus only on the component being tested
- 🚫 Avoid side effects – No real API calls, DB writes, or external service usage
- ⏱️ Improve test speed – Mocks are faster than real operations
- ⚙️ Control behavior – You decide what the mock returns (success, error, etc.)
// real function
function fetchUser(id) {
return axios.get(`/user/${id}`);
}
// test with mock
jest.mock('axios'); // mock axios
test('fetchUser returns data', async () => {
axios.get.mockResolvedValue({ data: { name: 'Bikram' } });
const response = await fetchUser(1);
expect(response.data.name).toBe('Bikram');
});
Type | Description |
---|---|
Mock | Fake with behavior you define/test |
Stub | Predefined responses (no logic) |
Spy | Tracks function calls and arguments |
Fake | Simpler implementation of a real one |
Dummy | Passed but not used (e.g., null obj) |