Testing & API Blueprint
This blueprint covers the standardized methods for testing your application and interacting with its communication layers (REST, GraphQL, and Kafka).
Quality & Standards
Your generated project includes pre-configured tools for automated testing and code quality.
1. Linting & Formatting
Ensure your code adheres to project standards:
# Lint check
npm run lint
# Auto-format
npm run format# Lint check
pnpm lint
# Auto-format
pnpm format# Lint check
yarn lint
# Auto-format
yarn format2. Unit & Integration Testing
Maintain high reliability with automated test suites:
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage# Run all tests
pnpm test
# Run tests with coverage
pnpm test:coverage# Run all tests
yarn test
# Run tests with coverage
yarn test:coverageAPI Interaction
Depending on your configuration, your microservice provides interactive documentation for exploring and testing endpoints. Choose your setup below:
# URL: http://localhost:3000/api-docs
# Standard Endpoints:
- GET /api/users: List all users.
- POST /api/users: Create a new user.
- PATCH /api/users/:id: Partially update a user.
- DELETE /api/users/:id: Delete a user (Soft Delete).# URL: http://localhost:3000/graphql
# Example Query (Get Users):
query GetAllUsers {
getAllUsers {
id
name
email
}
}
# Example Mutation (Create User):
mutation CreateUser {
createUser(name: "John Doe", email: "john@example.com") {
id
name
email
}
}# 1. Ensure infrastructure is running
docker-compose up -d kafka
# 2. Start the app
npm run dev (or use pnpm / yarn)
# 3. Trigger an event (via Postman or curl)
curl -X POST http://localhost:3000/api/users \
-H "Content-Type: application/json" \
-d '{"name": "Kafka Tester", "email": "kafka@example.com"}'
# 4. Observe the application logs:
[Kafka] Producer: Sent USER_CREATED event for 'kafka@example.com'
[Kafka] Consumer: Received USER_CREATED.
[Kafka] Consumer: Sending welcome email to 'kafka@example.com'... Done!# URL: http://localhost:3000/admin/queues
# Provided by Bull-Board, this interactive UI allows you to:
- Monitor active, delayed, and completed jobs.
- Retry failed background jobs manually.
- Pause and resume queues.Kafka Asynchronous Flow
When using Kafka, the project follows a non-blocking, event-driven pattern:
- Trigger: User makes a request (e.g.,
POST /api/users). - Produce: The API sends a
USER_CREATEDevent to the Kafka Broker. - Consume: The Welcome Worker (Consumer) picks up the event from the topic.
- Execute: The worker simulates sending an email in the background.
Background Jobs (BullMQ)
When using Background Jobs, the project implements a robust Redis-based queue system:
- Queue: Jobs are added to queues (e.g.,
emailQueue) via API endpoints or internal services. - Worker: Background workers process these jobs asynchronously to ensure the main event loop isn't blocked.
- Monitoring: Visit
http://localhost:3000/admin/queuesto access the Bull-Board Dashboard. Here you can inspect job statuses, retry failures, and monitor queue health in real time.
Verification Steps:
- Produce Event: POST to
http://localhost:3000/api/users. - Verify Logs: Look for
[Kafka] Producer: Sent USER_CREATED event. - Confirm Execution: Look for
[Kafka] Consumer: Received USER_CREATEDand email simulation.
Next Steps
If you have authentication enabled, check out the Authentication to learn how to secure your endpoints and authenticate your requests.