Created
March 30, 2024 13:15
-
-
Save carloswm85/6e341b286cd235e644e538ea4c6a80c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MongoClient, ObjectId } = require('mongodb'); | |
describe('insert', () => { | |
let connection; | |
let db; | |
beforeAll(async () => { | |
connection = await MongoClient.connect(globalThis.__MONGO_URI__, { | |
useNewUrlParser: true, | |
useUnifiedTopology: true | |
}); | |
console.log(`globalThis.__MONGO_URI__: ${globalThis.__MONGO_URI__}`); | |
db = await connection.db(globalThis.__MONGO_DB_NAME__); | |
console.log(`globalThis.__MONGO_DB_NAME__: ${globalThis.__MONGO_DB_NAME__}`); | |
}); | |
afterAll(async () => { | |
await connection.close(); | |
}); | |
it('should insert a doc into collection', async () => { | |
const users = db.collection('users'); | |
const objectId = new ObjectId(); | |
const mockUser = { _id: objectId, name: 'John' }; | |
await users.insertOne(mockUser); | |
const insertedUser = await users.findOne({ _id: objectId }); | |
expect(insertedUser).toEqual(mockUser); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment