UNPKG

1.15 kBJavaScriptView Raw
1const {MongoClient} = require('mongodb');
2
3describe('insert', () => {
4 const uri = global.__MONGO_URI__;
5 let connection;
6 let db;
7
8 beforeAll(async () => {
9 connection = await MongoClient.connect(uri, {
10 useNewUrlParser: true,
11 useUnifiedTopology: true,
12 });
13 db = await connection.db();
14 });
15
16 afterAll(async () => {
17 await connection.close();
18 });
19
20 it('should insert a doc into collection', async () => {
21 const users = db.collection('users');
22
23 const mockUser = {_id: 'some-user-id', name: 'John'};
24 await users.insertOne(mockUser);
25
26 const insertedUser = await users.findOne({_id: 'some-user-id'});
27
28 expect(insertedUser).toEqual(mockUser);
29 });
30
31 it('should insert many docs into collection', async () => {
32 const users = db.collection('users');
33
34 const mockUsers = [{name: 'Alice'}, {name: 'Bob'}];
35 await users.insertMany(mockUsers);
36
37 const insertedUsers = await users.find().toArray();
38
39 expect(insertedUsers).toEqual([
40 expect.objectContaining({name: 'John'}),
41 expect.objectContaining({name: 'Alice'}),
42 expect.objectContaining({name: 'Bob'}),
43 ]);
44 });
45});