Testcontainers 328,662 starts a throwaway Docker 514 container per test run and stops it afterwards. For MongoDB 1,815 that is @testcontainers/mongodb (github.com/testcontainers/testcontainers-node (https://github.com/testcontainers/testcontainers-node 2,617 ), MIT, version 12.1.0). You pin the exact server image your cluster runs, and the same tool covers everything else the suite touches — Redis 2,763 , S3, an SMTP sink.
import { MongoDBContainer } from '@testcontainers/mongodb';
export async function startDatabase() {
container = await new MongoDBContainer('mongo:8.3').start();
// the module runs mongod with --replSet and initiates it, so transactions work
await mongoose.connect(container.getConnectionString(), { directConnection: true });
}
export const stopDatabase = async () => {
await mongoose.disconnect(); await container.stop();
};directConnection: true is required: the container advertises a replica-set host name that does not resolve from the host machine, and without the flag topology discovery times out. That listing is configuration only — the machine used here has Docker CLI 29.6.2 but no running daemon, so this suite was not executed; every other listing in Seeding and Testing was.
| mongodb-memory-server 2,853 | Testcontainers | |
|---|---|---|
| Needs Docker | no | yes |
| Startup | 0.6-0.8 s | seconds, image pull on first run |
| Server version | downloaded binary | any Docker tag |
| Replica set | opt-in | default |
| Other services | MongoDB only | anything with an image |
Use the memory server for the suite that runs on every save, and Testcontainers where a test must exercise the exact server build or several services at once. Both hide behind the same two functions, so switching is one import.