Skip to main content
Testing is crucial for maintaining reliable applications. Esix provides excellent testing capabilities through its mock adapter, allowing you to test your models and queries without requiring a real MongoDB connection.

Mock Adapter Setup

Esix includes a mock adapter that simulates MongoDB operations in memory. This is perfect for unit tests and integration tests.

Configuration

Set up the mock adapter in your test environment:

Environment Variables

For testing, set these environment variables:

Testing Philosophy: In-Memory Database vs Mocking

We strongly recommend using an in-memory test database (like Esix’s mock adapter) instead of mocking the database connection or repository layer. This approach provides several key advantages:

Why In-Memory Databases Are Better

More Realistic Testing: In-memory databases test your actual application flows, including query logic, data transformations, and business rules, rather than just testing that mocked methods are called correctly. Full Integration Coverage: You can test complete user workflows from request to response, ensuring that all layers of your application work together properly. Easier Maintenance: No need to maintain complex mock setups that mirror your database schema and behavior. The in-memory database handles this automatically. Confidence in Refactoring: When you refactor your data access patterns, your tests continue to work without requiring updates to mock expectations.

Example: Testing User Registration

Real MongoDB contracts

The mock adapter does not reproduce every BSON, index, or driver behavior. Test those contracts with a real MongoDB server as well. In this repository, run:
This enables the real-server suite alongside the fast tests. It creates and drops its own uniquely named test database. CI runs this suite with MongoDB 6 and 8. Use a disposable test server. Without the URI, the real-server suite is skipped. Scalar reads (pluck) return stored values without model defaults. Numeric aggregates reject missing, nonnumeric and non-finite values; empty sets return zero. Include these boundaries in application tests. For Effect tests, see providing a borrowed database Layer.