a mongoose helper for using testcontainers with mongodb
https://www.npmjs.com/package/testcontainers-mongoose
npm i -D testcontainers-mongoose
if using mongoose 7.x
version
npm i -D testcontainers-mongoose@2.2.2
DEBUG=testcontainers
Enable testcontainers logsDEBUG=testcontainers:containers
Enable container logsDEBUG=testcontainers*
Enable all logs
TESTCONTAINERS_RYUK_DISABLED=true
Disable ryukRYUK_CONTAINER_IMAGE=registry.mycompany.com/mirror/ryuk:0.3.0
Custom image for ryuk
Testing with Vitest
import * as dbHandler from 'testcontainers-mongoose'
import { describe, beforeAll, afterAll, afterEach, it, expect } from 'vitest'
describe('testcontainers-mongoose test', () => {
beforeAll(async () => {
await dbHandler.connect('harbor.yourcompany.com/mongo:4.4.4')
// ...
})
afterAll(async () => {
await dbHandler.closeDatabase()
})
afterEach(async () => {
await dbHandler.clearDatabase()
})
it('some test using mongoose', async () => {
// ...
})
})
In case you want to connect the mongo by yourself
using startedMongoTestContainerOf
Get the mongo connection string by .getUri()
Example
import { startedMongoTestContainerOf, StartedMongoTestContainer } from 'testcontainers-mongoose'
import { describe, beforeAll, afterAll, afterEach, it, expect } from 'vitest'
describe('testcontainers-mongoose test', () => {
let mongoTestContainer: StartedMongoTestContainer
beforeAll(async () => {
mongoTestContainer = await startedMongoTestContainerOf('harbor.yourcompany.com/mongo:4.4.4')
// connect to mongoTestContainer by yourself
// mongoTestContainer.getUri() is the connection string of the mongo container
})
afterAll(async () => {
await mongoTestContainer.closeDatabase()
})
afterEach(async () => {
await mongoTestContainer.clearDatabase()
})
it('some test case', async () => {
// ...
})
})