forked from shelfio/jest-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update to latest jest & fix related problems
- Loading branch information
1 parent
3763fa8
commit 2e7133a
Showing
6 changed files
with
95 additions
and
75 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ temp | |
yarn.lock | ||
*.log | ||
.DS_Store | ||
globalConfig.json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,39 @@ | ||
const {MongoClient} = require('mongodb'); | ||
|
||
let connection; | ||
let db; | ||
describe('insert', () => { | ||
let connection; | ||
let db; | ||
|
||
beforeAll(async () => { | ||
connection = await MongoClient.connect(global.__MONGO_URI__); | ||
db = await connection.db(global.__MONGO_DB_NAME__); | ||
}); | ||
beforeAll(async () => { | ||
connection = await MongoClient.connect(global.__MONGO_URI__); | ||
db = await connection.db(global.__MONGO_DB_NAME__); | ||
}); | ||
|
||
afterAll(async () => { | ||
await connection.close(); | ||
await db.close(); | ||
}); | ||
afterAll(async () => { | ||
await connection.close(); | ||
await db.close(); | ||
}); | ||
|
||
it('should aggregate docs from collection', async () => { | ||
const files = db.collection('files'); | ||
it('should aggregate docs from collection', async () => { | ||
const files = db.collection('files'); | ||
|
||
await files.insertMany([ | ||
{type: 'Document'}, | ||
{type: 'Video'}, | ||
{type: 'Image'}, | ||
{type: 'Document'}, | ||
{type: 'Image'}, | ||
{type: 'Document'} | ||
]); | ||
await files.insertMany([ | ||
{type: 'Document'}, | ||
{type: 'Video'}, | ||
{type: 'Image'}, | ||
{type: 'Document'}, | ||
{type: 'Image'}, | ||
{type: 'Document'} | ||
]); | ||
|
||
const topFiles = await files | ||
.aggregate([{$group: {_id: '$type', count: {$sum: 1}}}, {$sort: {count: -1}}]) | ||
.toArray(); | ||
const topFiles = await files | ||
.aggregate([{$group: {_id: '$type', count: {$sum: 1}}}, {$sort: {count: -1}}]) | ||
.toArray(); | ||
|
||
expect(topFiles).toEqual([ | ||
{_id: 'Document', count: 3}, | ||
{_id: 'Image', count: 2}, | ||
{_id: 'Video', count: 1} | ||
]); | ||
expect(topFiles).toEqual([ | ||
{_id: 'Document', count: 3}, | ||
{_id: 'Image', count: 2}, | ||
{_id: 'Video', count: 1} | ||
]); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
const {MongoClient} = require('mongodb'); | ||
|
||
let connection; | ||
let db; | ||
|
||
beforeAll(async () => { | ||
connection = await MongoClient.connect(global.__MONGO_URI__); | ||
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); | ||
}); | ||
|
||
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'}) | ||
]); | ||
describe('insert', () => { | ||
let connection; | ||
let db; | ||
|
||
beforeAll(async () => { | ||
connection = await MongoClient.connect(global.__MONGO_URI__); | ||
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); | ||
}); | ||
|
||
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'}) | ||
]); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,28 @@ | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const MongodbMemoryServer = require('mongodb-memory-server'); | ||
const globalConfigPath = path.join(__dirname, 'globalConfig.json'); | ||
|
||
const MONGO_DB_NAME = 'jest'; | ||
const mongod = new MongodbMemoryServer.default({ | ||
instance: { | ||
dbName: MONGO_DB_NAME | ||
dbName: 'jest' | ||
}, | ||
binary: { | ||
version: '3.2.19' | ||
version: '3.2.18' | ||
} | ||
}); | ||
|
||
module.exports = function() { | ||
module.exports = async function() { | ||
const mongoConfig = { | ||
mongoDBName: 'jest', | ||
mongoUri: await mongod.getConnectionString() | ||
}; | ||
|
||
// Write global config to disk because all tests run in different contexts. | ||
fs.writeFileSync(globalConfigPath, JSON.stringify(mongoConfig)); | ||
console.log('Config is written'); | ||
|
||
// Set reference to mongod in order to close the server during teardown. | ||
global.__MONGOD__ = mongod; | ||
global.__MONGO_DB_NAME__ = MONGO_DB_NAME; | ||
process.env.MONGO_URL = mongoConfig.mongoUri; | ||
}; |