Engineering
encore-testing avatar

encore-testing

Unit and integration test your Encore.ts backend applications using Vitest, including support for isolated test databases and service mocking.

Introduction

This skill provides comprehensive instructions for testing backend services built with Encore.ts. It focuses on using Vitest as the primary testing framework, enabling developers to write high-quality, reliable code through automated unit and integration tests. The skill covers the standard setup process, including configuring Vitest to handle Encore-specific imports and integrating tests with Encore's local infrastructure management. By leveraging the encore test command, developers can ensure that each test run benefits from automatic test database provisioning, isolated environment management, and proper service dependency handling.

  • Write unit tests for API endpoints by calling them directly as TypeScript functions, allowing for easy verification of request-response contracts.
  • Implement isolated database operations where each test benefits from a clean, transactional state, automatically rolled back upon completion to ensure side-effect-free test runs.
  • Utilize Vitest's powerful mocking capabilities to isolate service-to-service communication or to substitute external third-party API dependencies.
  • Handle asynchronous test scenarios, including complex error case verification using APIError codes and validating Pub/Sub event publication.
  • Test cron jobs and background tasks by directly invoking the underlying handler functions within a controlled test environment.
  • Configure advanced test settings including Vitest globals, node environment isolation, and coverage reporting tools.
  • Integrate tests into VS Code using the Vitest extension, with specific guidance on managing file-level parallelism to prevent port conflicts during test execution.
  • Optimize CI/CD pipelines by utilizing appropriate flag configurations for parallel execution versus local development workflows.

Follow the established patterns to ensure your test suite remains maintainable. Always prefer using the real Encore infrastructure components like PostgreSQL databases or Pub/Sub queues whenever possible rather than mocking them, to maintain high-fidelity integration tests. Use the provided configurations for vite.config.ts to ensure proper alias resolution for generated code. Keep your tests focused on business logic and service interactions, ensuring full path coverage for error scenarios and successful execution flows.

Repository Stats

Stars
23
Forks
3
Open Issues
1
Language
Not provided
Default Branch
main
Sync Status
Idle
Last Synced
May 3, 2026, 06:45 PM
View on GitHub