testcontainers-mongoose
v4.2.0
Published
Testcontainer mongo with mongoose
Downloads
109
Maintainers
Readme
Testcontainers-mongoose
a mongoose helper for using testcontainers with mongodb
https://www.npmjs.com/package/testcontainers-mongoose
Installation
npm i -D testcontainers-mongoose
if using mongoose 7.x
version
npm i -D [email protected]
Configuration
Logs
DEBUG=testcontainers
Enable testcontainers logsDEBUG=testcontainers:containers
Enable container logsDEBUG=testcontainers*
Enable all logs
Testcontainers
TESTCONTAINERS_RYUK_DISABLED=true
Disable ryukRYUK_CONTAINER_IMAGE=registry.mycompany.com/mirror/ryuk:0.3.0
Custom image for ryuk
Usage
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 () => {
// ...
})
})
Start Mongo Container without connect with mongoose
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 () => {
// ...
})
})