-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(integration-examples): add example on how to use with "jest"
closes #319
- Loading branch information
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
--- | ||
id: integration-examples | ||
title: 'Integration Examples' | ||
--- | ||
|
||
This Guide will show how `mongodb-memory-server` can be used with different frameworks | ||
|
||
## jest | ||
|
||
For useage with `jest` it is recommended to use the [`globalSetup`](https://jestjs.io/docs/en/configuration#globalsetup-string) and [`globalTeardown`](https://jestjs.io/docs/en/configuration#globalteardown-string) options | ||
|
||
`jest.config.json`: | ||
|
||
```ts | ||
{ | ||
"preset": "ts-jest", | ||
"globalSetup": "<rootDir>/test/globalSetup.ts", | ||
"globalTeardown": "<rootDir>/test/globalTeardown.ts", | ||
"setupFilesAfterEnv": [ | ||
"<rootDir>/test/setupFile.ts" | ||
] | ||
} | ||
|
||
``` | ||
|
||
`globalSetup.ts`: | ||
|
||
```ts | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import * as mongoose from 'mongoose'; | ||
import { config } from './utils/config'; | ||
|
||
export = async function globalSetup() { | ||
if (config.Memory) { // Config to decided if an mongodb-memory-server instance should be used | ||
// it's needed in global space, because we don't want to create a new instance every test-suite | ||
const instance = await MongoMemoryServer.create(); | ||
const uri = instance.getUri(); | ||
(global as any).__MONGOINSTANCE = instance; | ||
process.env.MONGO_URI = uri.slice(0, uri.lastIndexOf('/')); | ||
} else { | ||
process.env.MONGO_URI = `mongodb://${config.IP}:${config.Port}`; | ||
} | ||
|
||
// The following is to make sure the database is clean before an test starts | ||
await mongoose.connect(`${process.env.MONGO_URI}/${config.DataBase}`, { useNewUrlParser: true, useUnifiedTopology: true }); | ||
await mongoose.connection.db.dropDatabase(); | ||
await mongoose.disconnect(); | ||
}; | ||
|
||
``` | ||
|
||
`globalTeardown.ts`: | ||
|
||
```ts | ||
import { MongoMemoryServer } from 'mongodb-memory-server'; | ||
import { config } from './utils/config'; | ||
|
||
export = async function globalTeardown() { | ||
if (config.Memory) { // Config to decided if an mongodb-memory-server instance should be used | ||
const instance: MongoMemoryServer = (global as any).__MONGOINSTANCE; | ||
await instance.stop(); | ||
} | ||
}; | ||
``` | ||
|
||
and an [`setupFilesAfterEnv`](https://jestjs.io/docs/en/configuration#setupfilesafterenv-array) can be used to connect something like `mongoose` or `mongodb` | ||
|
||
`setupFile.ts`: | ||
|
||
```ts | ||
import { connect, disconnect } from './utils/connect'; | ||
|
||
beforeAll(async () => { | ||
await connect(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await disconnect(); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters