Skip to content

Conversation

@raza-khan0108
Copy link

Overview

This PR introduces the initial testing infrastructure for the project, addressing the "No tests are available" status noted in the README. It specifically focuses on unit testing the service layer to ensure business logic is decoupled from database dependencies.

Changes Made

  • Testing Framework Configuration: Leveraged existing jest and supertest dev-dependencies to establish a standardized testing environment.
  • User Service Unit Tests: Created server/test/controller/user.service.test.js to validate the getUsers logic.
  • Mongoose Mocking: Implemented jest.mock for the User model to facilitate isolated testing of the service layer without requiring a live MongoDB connection or impacting the MONGODB_URI.
  • Babel Integration: Integrated with the existing @babel/preset-env configuration to allow ES Module syntax (import/export) within test files, maintaining consistency with the server/src codebase.

Testing Coverage

The new test suite covers the following scenarios for server/src/service/user.service.js:

  1. Fetch All Users: Ensures User.find() is called without arguments when no ID is provided.
  2. Fetch Specific User: Validates that User.find({ id }) is called with the correct filter when a specific ID is passed.
  3. Empty States: Verifies the service correctly returns an empty array when no records exist in the database.

Technical Details

  • Command to Run Tests: npm test.
  • Mocking Library: Jest.
  • Architecture: Follows the Arrange-Act-Assert (AAA) pattern for clear and maintainable test cases.

Checklist

  • Service logic isolated from database.
  • All tests passing locally.
  • ES6+ syntax transpiled via Babel.
  • No side effects on production data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant