← Effect fixes your TypeScript

Effect fixes your TypeScript · Part 7

You don't need a DI framework

· by Tony Tang

Importing db directly means jest.mock with path strings in tests. In Effect, the database is a service that shows up in the type. Provide the real layer in production and a fake one in tests, with no mocking library.

Comment below: How do you mock your database today?

Effect fixes your TypeScript: a series on the bugs you already ship, and how Effect makes them impossible. All code is real Effect v4 and was compiled before it went on screen. https://effect.website

Transcript

You don't need a DI framework. Your types can do it. getUser imports Db directly. It works until you test it. Then it's jest.mock with a file path string, a mocking library, and tests that break when you rename a folder. The dependency is real, but it's invisible in the signature. In Effect, Db is a service. Use it and it shows up in the type. Effect<User, the errors, Db>. That third slot is what the function needs to run. Forget to provide it, compile error. In production, provide the real layer. In tests, a fake one. In four lines. No mocking library, no path strings. Best practice, dependencies belong in the type, so swapping them is a value, not a hack. But your real DB opens connections. Who closes them when things go wrong? Part eight. How do you mock your database today?

Full lesson in the Learn Effect course ↗ Services and the requirements channel

More from this series