Just want to make sure, but it seems like I can sum up your reasons for mocking are:

- spinning up the service is hard

- startup times can be inconsistent

I may have not read deep enough but I don't see much explanation around the "I need A to behave like X" -- could you give a more concrete example? Lets say you had two queue implementations and one used Redis and one used RabbitMQ (i.e `GenericQueue<Redis|Rabbit>`), it seems ridiculous to need one of them to act like the other -- I'd love an example if you have one in mind.

Optimizing spinning up the service and making startup times consistent are basically solved in half a day of work (that pays off forever), in my experience. Here's an excerpt from a Gitlab CI config I carry around from project-to-project:

e2e:
stage: test
only:
- merge_requests
services:
- docker:dind
variables:
DOCKER_HOST: tcp://docker:2376 # TLS enabled (2375 for disabled)
DOCKER_TLS_CERTDIR: '/certs'
DOCKER_TLS_VERIFY: 1
DOCKER_CERT_PATH: '/certs/client'
script:
- make ensure-images setup build
- make test-e2e

The "ensure-images" make target actually just runs a `docker pull` before the tests:

ensure-images:
$(DOCKER) pull $(DB_IMAGE):$(DB_IMAGE_TAG)
$(DOCKER) pull $(MAILER_IMAGE):$(MAILER_IMAGE_TAG)
$(DOCKER) pull $(STRIPE_IMAGE):$(STRIPE_IMAGE_TAG)

Which actually reminds me, another cool thing you can mock if you do this -- it's not 100% parity but there's a cool stripe replacement called localstripe[0]. There are bugs/unimplemented features (expecting a random F/OSS project without a ton of members to replicate stripe's API is a lot) but it exists and isn't too hard to use, so I use it in test/local/dev environments, use stripe test API keys in staging, and the real keys in prod.

Another thing worth thinking is that how you optimize each piece is different -- it's often pretty fast to make new exchanges/databases and you can even just TRUNCATE tables if you're really worried about it. Here's an example from some code I wrote:

public resetForTest(): Promise<this> {
if (!this.conn) { return Promise.reject(new DatabaseDisconnectedError()); }

// Do not truncate if database is <production db name>
// for example when process.env.USE_LOCAL_DB_FOR_TEST is set
if (this.getDatabaseName() === "<production db name>") {
return Promise.resolve(this);
}

return this.getConnection()
.then(c => c.query(`
TRUNCATE TABLE <some table> CASCADE;
TRUNCATE TABLE <another table> CASCADE;
... more ...
`))
.then(() => this);
}

And this pattern extends itself to anything that is testable, so I've enshrined it:

export interface TestableComponent extends Component {
resetForTest(): Promise<this>;
}

Completely separate from all this -- some of the time you can literally just.. run these dependencies in memory[1]. Doesn't get much faster than that. I haven't run into a mock that didn't take time and effort to maintain/alter whenever code or the underlying systems changed. Time spent maintaining mocks is time wasted.

Anyway, I won't go as far as saying that mocks are never useful, sometimes a mock is the easiest and best way -- but I can say that I spend most of my time writing E2E tests, and I trust them a lot and they give me trust in my codebase much more than mocks or unit tests do. I barely write unit tests anymore to check edge cases, because if I really cared I'd just generate them -- and I don't really have to care because I use strong type systems where possible (Typescript for JS, I avoid PHP/Python/Ruby, Rust, Haskell).

Here comes the hot-take -- unit tests (and their rise in popularity/necessity) is actually a direct reflection of the adoption of non-compile-time-type-checked dynamically interpreted languages, and burdensome class-based "type systems". Correct usage of good, expressive and concise (where possible) type systems encourages you to make invalid/nonsensical states impossible, and people got excited about how fast you could churn out code (compared to Java most things feel pretty productive) and they started having numbers where they thought they had strings, and negative numbers where they thought they had natural numbers. Good type systems make it easy to make these cases impossible. As for the business stuff (never store decimals for currency, make sure your amounts are natural numbers, etc), you have to learn that with time/intuition built over time -- you don't know to write that unit test unless it's bit you before (though sitting down to write unit tests might tease it out of you).

I write unit tests as regression tests basically now, but I find I rarely have to do that, since most of the time when a weird case has gone through it's an indicator that I was too lose with the types.

[0]: https://github.com/adrienverge/localstripe

[1]: https://github.com/nodkz/mongodb-memory-server