Skip to content

Commit

Permalink
docs(integration-examples): add example on how to use with "jest"
Browse files Browse the repository at this point in the history
closes #319
  • Loading branch information
hasezoey committed Mar 8, 2021
1 parent fb4886b commit c310980
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
80 changes: 80 additions & 0 deletions docs/guides/integration-examples.md
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();
});
```
2 changes: 1 addition & 1 deletion website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ module.exports = {
"Classes": ["api/mongo-memory-server", "api/mongo-memory-replset", "api/mongo-instance", "api/mongo-binary"]
},
guides: {
"Getting Started": ["guides/quick-start-guide", "guides/error-warning-details", "guides/faq", "guides/supported-systems"]
"Getting Started": ["guides/quick-start-guide", "guides/error-warning-details", "guides/faq", "guides/supported-systems", "guides/integration-examples"]
}
};

0 comments on commit c310980

Please sign in to comment.