-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: add jest-mongodb example #5571
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b61efcb
docs: add jest-mongodb example
vladholubiev 513a6fb
docs: add connection.close() and remove escape hatch --forceExit
vladholubiev b01beb3
s/puppeteer/mongo
rickhanlonii 284098d
Add MongoDB guide to changelog
rickhanlonii 6b029f3
Merge remote-tracking branch 'upstream/master' into feature/docs-mongodb
rickhanlonii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,124 @@ | ||
--- | ||
id: mongodb | ||
title: Using with MongoDB | ||
--- | ||
|
||
With the [Global Setup/Teardown](Configuration.md#globalsetup-string) and | ||
[Async Test Environment](Configuration.md#testenvironment-string) APIs, Jest can | ||
work smoothly with [MongoDB](https://www.mongodb.com/). | ||
|
||
## A jest-mongodb example | ||
|
||
The basic idea is to: | ||
|
||
1. Spin up in-memory mongodb server | ||
2. Export a global variable with mongo URI | ||
3. Write tests for queries / aggregations using a real database ✨ | ||
4. Shut down mongodb server using Global Teardown | ||
|
||
Here's an example of the GlobalSetup script | ||
|
||
```js | ||
// setup.js | ||
const MongodbMemoryServer = require('mongodb-memory-server'); | ||
|
||
const MONGO_DB_NAME = 'jest'; | ||
const mongod = new MongodbMemoryServer.default({ | ||
instance: { | ||
dbName: MONGO_DB_NAME | ||
}, | ||
binary: { | ||
version: '3.2.19' | ||
} | ||
}); | ||
|
||
module.exports = function() { | ||
global.__MONGOD__ = mongod; | ||
global.__MONGO_DB_NAME__ = MONGO_DB_NAME; | ||
}; | ||
``` | ||
|
||
Then we need a custom Test Environment for puppeteer | ||
|
||
```js | ||
// mongo-environment.js | ||
class MongoEnvironment extends NodeEnvironment { | ||
constructor(config) { | ||
super(config); | ||
} | ||
|
||
async setup() { | ||
console.log('Setup MongoDB Test Environment'); | ||
|
||
this.global.__MONGO_URI__ = await global.__MONGOD__.getConnectionString(); | ||
this.global.__MONGO_DB_NAME__ = global.__MONGO_DB_NAME__; | ||
|
||
await super.setup(); | ||
} | ||
|
||
async teardown() { | ||
console.log('Teardown MongoDB Test Environment'); | ||
|
||
await super.teardown(); | ||
} | ||
|
||
runScript(script) { | ||
return super.runScript(script); | ||
} | ||
} | ||
``` | ||
|
||
Finally we can shut down mongodb server | ||
|
||
```js | ||
// teardown.js | ||
module.exports = async function() { | ||
await global.__MONGOD__.stop(); | ||
}; | ||
``` | ||
|
||
With all the things set up, we can now write our tests like this: | ||
|
||
```js | ||
// test.js | ||
const {MongoClient} = require('mongodb'); | ||
|
||
let connection; | ||
let db; | ||
|
||
beforeAll(async () => { | ||
connection = await MongoClient.connect(global.__MONGO_URI__); | ||
db = await connection.db(global.__MONGO_DB_NAME__); | ||
}); | ||
|
||
afterAll(async () => { | ||
await connection.close(); | ||
await db.close(); | ||
}); | ||
|
||
it('should aggregate docs from collection', async () => { | ||
const files = db.collection('files'); | ||
|
||
await files.insertMany([ | ||
{type: 'Document'}, | ||
{type: 'Video'}, | ||
{type: 'Image'}, | ||
{type: 'Document'}, | ||
{type: 'Image'}, | ||
{type: 'Document'} | ||
]); | ||
|
||
const topFiles = await files | ||
.aggregate([{$group: {_id: '$type', count: {$sum: 1}}}, {$sort: {count: -1}}]) | ||
.toArray(); | ||
|
||
expect(topFiles).toEqual([ | ||
{_id: 'Document', count: 3}, | ||
{_id: 'Image', count: 2}, | ||
{_id: 'Video', count: 1} | ||
]); | ||
}); | ||
``` | ||
|
||
Here's the code of | ||
[full working example](https://github.com/vladgolubev/jest-mongodb). |
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"es6-class-mocks", | ||
"webpack", | ||
"puppeteer", | ||
"mongodb", | ||
"migration-guide", | ||
"troubleshooting" | ||
], | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this say "for Mongo"?