forked from shelfio/jest-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo-insert.test.js
44 lines (33 loc) · 1.11 KB
/
mongo-insert.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const {MongoClient} = require('mongodb');
describe('insert', () => {
let connection;
let db;
beforeAll(async () => {
connection = await MongoClient.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
db = await connection.db();
});
afterAll(async () => {
await connection.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);
});
it('should insert many docs into collection', async () => {
const users = db.collection('users');
const mockUsers = [{name: 'Alice'}, {name: 'Bob'}];
await users.insertMany(mockUsers);
const insertedUsers = await users.find().toArray();
expect(insertedUsers).toEqual([
expect.objectContaining({name: 'John'}),
expect.objectContaining({name: 'Alice'}),
expect.objectContaining({name: 'Bob'})
]);
});
});