[DEMO] How the dependency container keeps services testable

Demo post with code.

Resolve, don't import

ts
const getDb = () => getContainer().get<DbInstance>(ServiceName.DB);

Swap in tests

Register a fake before the test and the service code never knows.

Keep getters at the top

A getter per service at the top of the file reads well and mocks well.

How did you like this article?

All articles