This repository demonstrates a bug in Effect's HttpApiMiddleware
where closures from previous layer instances are executed despite proper layer recreation in test environments.
When running multiple tests that create fresh database connections and HTTP server layers, HttpApiMiddleware
closures from previous test runs are being executed instead of the newly created middleware instances, despite logs confirming that the middleware layers are being properly recreated.
Related GitHub Issue: [Link to your Effect issue here]
- ✅ New middleware layers are created correctly (confirmed by logs)
- ✅ New database services are created with unique file paths
- ✅ Fresh HTTP server layers are instantiated for each test
- ❌ BUG: Old middleware closures execute instead of new ones
This causes tests to fail because the second test attempts to authenticate against the database from the first test, leading to "User not found" errors.
-
Clone the repository:
git clone <repo-url> cd effect-bug-repro
-
Install dependencies:
bun install
-
Run the test to reproduce the bug:
bun test
Each test should:
- Create a new
DatabaseService
with unique file path (e.g.,.volumes/uuid1
) - Create a new
AuthorizationLive
middleware instance - Execute the NEW middleware's closures during HTTP requests
Test 1:
Creating middleware 0.41260833894960913 with DB .../uuid1
Test 2:
Creating middleware 0.1647645200653125 with DB .../uuid2
When Test 2 middleware executes:
Executing middleware 0.41260833894960913 with DB .../uuid1 # ❌ OLD closure!
The middleware from Test 1 (0.41260833894960913
) executes instead of the new one (0.1647645200653125
), causing the authentication to query the wrong database.
tests/auth.test.ts
- Demonstrates the bug with sequential test runssrc/domains/auth/service.ts
- Contains theAuthorizationLive
middleware with debugging outputsrc/db/index.ts
- Database service that should be recreated for each test
The logs clearly show that while new middleware instances are created, the execution uses old closures:
# Test 1 middleware creation
0.41260833894960913 /path/to/db1
# Test 2 middleware creation
0.1647645200653125 /path/to/db2
# Test 2 middleware execution (WRONG!)
UU 0.41260833894960913 /path/to/db1
- Bun: v1.2.12
- Effect: 3.15.4
- @effect/platform: 0.82.7
- @effect/platform-bun: 0.65.4