Configuring Jest with MongoDB: A Step-by-Step Guide
Using with MongoDB
Using the Global Setup/Teardown and Async Test Environment APIs, Jest will be able to work smoothly with MongoDB.
Use jest-mongodb Preset
Jest MongoDB will provide all the required configuration to run your tests using MongoDB.
1. First you need to install @shelf/jest-mongodb by running the command below:
yarn add @shelf/jest-mongodb -dev
2. Then you can specify preset in your Jest configuration like so:
{
"preset": "@shelf/jest-mongodb"
}
3. Finally, you can write your test
const {MongoClient} = require('mongodb');
describe('insert', () => {
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__, {
useNewUrlParser: true,
});
db = await connection.db(global.__MONGO_DB_NAME__);
});
afterAll(async () => {
await connection.close();
await db.close();
});
it('should insert a doc into collection', async () => {
const users = db.collection('users');
const mockUser = {_id: 'some-user-id', name: 'John'};
await users.insertOne(mockUser);
const insertedUser = await users.findOne({_id: 'some-user-id'});
expect(insertedUser).toEqual(mockUser);
});
});
Let us consider the mocking the insertion of a post on a blog.
const {MongoClient} = require('mongodb');
describe('insert', () => {
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__, {
useNewUrlParser: true,
});
db = await connection.db(global.__MONGO_DB_NAME__);
});
afterAll(async () => {
await connection.close();
await db.close();
});
it('should insert a document into collection', async () => {
const posts = db.collection('posts');
const mockPost = {_id: 'some-post-id', name: 'Stocks'};
await posts.insertOne(mockPost);
const insertedPost = await posts.findOne({_id: 'some-post-id'});
expect(insertedPost).toEqual(mockPost);
});
});
Finally, let us consider the addition of a product to a product listing page.
const {MongoClient} = require('mongodb');
describe('insert', () => {
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(global.__MONGO_URI__, {
useNewUrlParser: true,
});
db = await connection.db(global.__MONGO_DB_NAME__);
});
afterAll(async () => {
await connection.close();
await db.close();
});
it('should insert a document into collection', async () => {
const products = db.collection('products');
const mockProduct = {_id: 'some-product-id', name: 'Shirts'};
await products.insertOne(mockProduct);
const insertedProduct = await products.findOne({_id: 'some-product-id'});
expect(insertedProduct).toEqual(mockProduct);
});
});
We have demonstrated how to mock a MongoDB database with three examples that inserts a document into a collection.
Notice that each of the examples above there is no need to load any dependencies.
Previous:
Using Jest with DynamoDB: Configuration and Testing Guide.
Next:
Using Jest with Puppeteer: Configuration and Testing Guide.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/jest/using-with-mongodb.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics