Last active
July 7, 2022 12:28
-
-
Save damdafayton/9f6efbc26df59434e1401a0594f9d45a to your computer and use it in GitHub Desktop.
Jest doesnt terminate after successfull test if mongodb store is used in express sessions
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 MongoStore = require('connect-mongo'); | |
const express = require('express'); | |
const session = require('express-session'); | |
const { MongoMemoryServer } = require('mongodb-memory-server'); | |
const mongoose = require('mongoose'); | |
const request = require('supertest'); | |
const app = express(); | |
// This will create an new instance of "MongoMemoryServer" and automatically start it | |
let mongod; | |
/** | |
* Connect to the in-memory database. | |
*/ | |
const connect = async () => { | |
mongod = await MongoMemoryServer.create(); | |
const uri = mongod.getUri(); | |
const mongooseOpts = { | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
}; | |
await mongoose.connect(uri, mongooseOpts); | |
return uri; | |
}; | |
/** | |
* Drop database, close the connection and stop mongod. | |
*/ | |
const closeDatabase = async () => { | |
await mongoose.connection.dropDatabase(); | |
await mongoose.connection.close(); | |
await mongod.stop(); | |
}; | |
/** | |
* Remove all the data for all db collections. | |
*/ | |
const clearDatabase = async () => { | |
const collections = mongoose.connection.collections; | |
for (const key in collections) { | |
const collection = collections[key]; | |
await collection.deleteMany(); | |
} | |
}; | |
app.get('/user', async function (req, res) { | |
const response = await new Promise((resolve) => resolve({ foo: 'bar' })); | |
res.status(200).json(response); | |
}); | |
describe('GET /user', function () { | |
beforeAll(async () => { | |
const uri = await connect(); | |
console.log(uri); | |
let store; | |
// store = MongoStore.create({ | |
// mongoUrl: uri, | |
// touchAfter: 24 * 60 * 60, | |
// }); | |
app.use( | |
session({ | |
store, | |
secret: 'foo, bar', | |
name: 'Mock-session', | |
resave: false, | |
saveUninitialized: true, | |
cookie: { | |
sameSite: 'lax', // lax or strict | |
secure: process.env.NODE_ENV === 'production', | |
httpOnly: true, | |
maxAge: 3600000 * 24 * 7, | |
}, | |
}) | |
); | |
}); | |
/** | |
* Clear all test data after every test. | |
*/ | |
afterEach(async () => await clearDatabase()); | |
/** | |
* Remove and close the db and server. | |
*/ | |
afterAll(async () => await closeDatabase()); | |
it('responds with json', async function () { | |
const response = await request(app) | |
.get('/user') | |
.set('Accept', 'application/json'); | |
console.log(Object.keys(response)); | |
console.log(response.headers); | |
console.log(response.body); | |
expect(response.headers['content-type']).toMatch(/json/); | |
expect(response.status).toEqual(200); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment