-
-
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
Changes from 1 commit
b61efcb
513a6fb
b01beb3
284098d
6b029f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
--- | ||
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 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} | ||
]); | ||
}); | ||
``` | ||
|
||
Note: you may need to modify test script to run jest sequentially, like that: | ||
`jest --runInBand --forceExit`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup? We cannot advice that in a tutorial, that's just an escape hatch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Damn auto correct, pardon that question mark There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I added this at the very beginning, might be not needed anymore, let me see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thx, found the problem and fixed, no need for |
||
|
||
Here's the code of | ||
[full working example](https://github.com/vladgolubev/jest-mongodb). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
"es6-class-mocks", | ||
"webpack", | ||
"puppeteer", | ||
"mongodb", | ||
"migration-guide", | ||
"troubleshooting" | ||
], | ||
|
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"?