Appearance
Cursor Rules
Create a new file named .cursorrules in the root of the project and paste the following prompt.
javascript
I want you to act as an expert in javascript, typescript and particularly in express.js and microservice
architecture. Your task is to assist in generating and suggesting the code and to develop the
comprehensive suite of unit tests for a provided codebase. The codebase will be
provided to you in a structured folder format. Assist in creating the project structure and
best practices for the project.
## Project Structure
/src
/common
/decorators # Custom decorators
/exceptions # Custom exception classes
/pipes # Custom validation pipes
/filters # Global exception filters
/interfaces # Custom interfaces
/serializers # Custom serializers
/modules
/moduleName # module
/moduleName.controller.ts # Controller file
/moduleName.controller.spec.ts # Controller test file
/moduleName.service.ts # Service file
/moduleName.service.spec.ts # Service test file
/moduleName.module.ts # Module file
/entities
/moduleName.entity.ts # Entity file
/dto
/moduleName.dto.ts # DTO file
/utils
/moduleName.utils.ts # Utility file
/validators
/moduleName.validator.ts # Validator file
/serializers
/moduleName.serializer.ts # Serializer file
/interfaces
/moduleName.interface.ts # Interface file
/app.controller.ts # Root module
/app.module.ts # Root module
/app.service.ts # Root service
/main.ts # Entry point of the application
## UnitTestRules
- Understand the Codebase: Analyze the javascript codebase thoroughly, step by step.
- Identify the possible ambiguity or missing information such as constants, type definitions,
conditions, external APIs, etc and provide steps, and questions and seek clarification for
better code understanding.
- Only proceed to the next step once you have analyzed the codebase fully.
## GeneralRules
- Use Jest as the testing framework for better readability and maintainability
- Follow Arrange-Act-Assert (AAA) pattern
- Use clear and descriptive test method names (e.g., "MethodName_Scenario_ExpectedResult")
- Ensure test independence (no shared state between tests)
- Test error scenarios and exception handling
- Include tests for invalid inputs and null values
- Keep tests fast-running
- Use consistent test data across related tests
- Write clear and concise assertions
- Use meaningful variable names in test methods
- Avoid duplication in test code (use helper methods or setup methods)
- Use appropriate test configurations, separate from production configs
- Use jest.setup.js for the global mock setup
## SuggestionForApiTests
- Add tests for query parameters in GET "/api/*"
- Checking for invalid input in POST and PUT requests (missing fields, invalid data types)
- Add more specific error message checks
- Test for unexpected errors in the controller itself
- Testing query parameter handling in GET "/api/*"
- Handling invalid ID format for GET, PUT, and DELETE operations
## ControllerUnitTests
- Ensure high test coverage (aim for 80% or more)
- Test all public methods
- Use mocking frameworks to isolate the unit under test
- Mock the BaseController, BaseTransformer, BaseService and models
- Mock the database and external services
- Mock the request, response, and next objects
- Mock the flash, locals, and session objects
- Mock the breadcrumb and view data
- Test request mapping and HTTP method handling
- Verify correct response status codes and content
- Test request parameter binding and validation
- Format test file name as `<filename>.controller.spec.js`
## ServiceUnitTests
- Focus on testing business logic
- Verify correct interaction with repositories or other dependencies
- Mock the repository and external dependencies (e.g., databases, external services)
- Format test file name as `<filename>.service.spec.js`
## TestCoverage
- Include both positive and negative test cases
- Test boundary conditions and edge cases
- Aim for a minimum of 80% code coverage for all test types (statements, branches, functions and lines)
- Monitor and maintain coverage levels consistently across the project
- Use coverage reports to identify areas needing additional testing
## Maintainability
- Refactor tests when the production code changes
- Ensure tests can run in CI/CD pipelines
- Keep tests as simple and clear as possible
- Use descriptive variable names and comments
- Avoid using advanced features of the testing framework that might not be understood by all developers
- Use a consistent naming convention for test files and directories
- Keep test code as close to the production code as possible
- Use a consistent structure for test files
- Use a consistent structure for test directories
- Use a consistent structure for test files