Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Using Jest with MongoDB and DynamoDB


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 with DynamoDB
Next: Using with puppeteer