Skip to content

raibikram/Nodejs-Testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation


Testing

Testing is the process of checking software to ensure it works correctly, meets requirements, and is free of bugs.


📌 Why Testing is Important (Key Points):

  1. Finds bugs early
  2. Ensures software quality
  3. Prevents crashes and failures
  4. Saves time and cost
  5. Improves security
  6. Builds confidence
  7. Enhances user experience
  8. Validates requirements

Some types:

1. Unit Testing

  • 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.

🔗 2. Integration Testing

  • 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.

🌐 3. End-to-End (E2E) Testing

  • 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.

some Nodejs testing library

  • jest
  • moka
  • Nodejs build-in

Mocking

What is Mocking?

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

📌 Why Use Mocking?

  1. Test in isolation – Focus only on the component being tested
  2. 🚫 Avoid side effects – No real API calls, DB writes, or external service usage
  3. ⏱️ Improve test speed – Mocks are faster than real operations
  4. ⚙️ Control behavior – You decide what the mock returns (success, error, etc.)

🧪 Example in JavaScript (Jest):

// 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');
});

🔁 Types of Test Doubles:

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)

About

Nodejs Testing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published