You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to make use of an in-memory database (mongodb-memory-server) when running my tests, but I can't get it to work. I think that the fact that I'm requiring "loaders" when I create a server instance in the tests creates a mongoose connection with the actual database (mongoose.ts is one of the loaders). So I'm unable to connect with the test database.
I've gone through the "test-related" issues on this repo, and all of them seemed to make use of mocks, but I don't want to do that.
I'd appreciate any suggestions on how to resolve this---how to essentially, given this project structure, make use of a test database such as mongodb-memory-server.
Here's what my db-manager looks like - it houses the methods required to create the test DB.
importmongoosefrom"mongoose";import{MongoMemoryServer}from"mongodb-memory-server"letmongod: MongoMemoryServer;asyncfunctioncreateMongoDB(){if(!mongod){mongod=awaitMongoMemoryServer.create({binary: {version: "5.0.8"}})}}/** * Connect to the in-memory database. */constconnect=async()=>{awaitcreateMongoDB()consturi=mongod.getUri()process.env.databaseURI=uriconstconnection=mongoose.connect(uri,{dbName: "riteshop-test-db"});returnconnection}/** * Clear the in-memory database. */constclear=async()=>{constcollections=mongoose.connection.collections;for(constkeyincollections){awaitcollections[key].deleteMany({});}};/** * Disconnect from in-memory database. */constdisconnect=async()=>{awaitmongoose.connection.dropDatabase()awaitmongoose.connection.close()awaitmongod.stop()}exportdefault{ connect, clear, disconnect }
Also, here is what my test looks like
importrequestfrom"supertest"importdbfrom"../db-manager"importconfigfrom"../../../src/config"importexpressfrom"express"describe("Health Check",()=>{constapp=express()beforeEach(async()=>{awaitdb.connect()// requiring the loaders is necessary because that's how the server is started normally// but that also means the db connection in `src/loaders/mongoose` would be initialized/attempted.// I don't want it attempted but it sorts of needs to be because the connection is needed by the dependency injector.awaitrequire("../../../src/loaders").default({expressApp: app})app.listen(config.testPort)})afterEach(async()=>{awaitdb.clear()awaitdb.disconnect()})describe('Endpoints availability',()=>{it('should return 404',async()=>{constres=awaitrequest(app).get('/api/nonexisting').send()expect(res.status).toEqual(404)})})})
UPDATE: A few mins later
What I do now, which seems to work, is to
first, disconnect from the main db instance
then connect to the test db instance
constapp=express()beforeEach(async()=>{awaitrequire("../../../src/loaders").default({expressApp: app})// disconecting from main DBmongoose.connection.close()// connecting to test DBawaitdb.connect()app.listen(config.testPort)})
I'm hoping there's a much neater way to go about it. Furthermore, I'm not sure if this approach is consequence-free as the instance I had to disconnect from is what was passed to the dependencyInjectorLoader, and subsequently, the agendaInstance as seen in the repo's source code.
The text was updated successfully, but these errors were encountered:
I'm trying to make use of an in-memory database (mongodb-memory-server) when running my tests, but I can't get it to work. I think that the fact that I'm requiring "loaders" when I create a server instance in the tests creates a mongoose connection with the actual database (
mongoose.ts
is one of the loaders). So I'm unable to connect with the test database.I've gone through the "test-related" issues on this repo, and all of them seemed to make use of mocks, but I don't want to do that.
I'd appreciate any suggestions on how to resolve this---how to essentially, given this project structure, make use of a test database such as mongodb-memory-server.
Here's what my
db-manager
looks like - it houses the methods required to create the test DB.Also, here is what my test looks like
UPDATE: A few mins later
What I do now, which seems to work, is to
I'm hoping there's a much neater way to go about it. Furthermore, I'm not sure if this approach is consequence-free as the instance I had to disconnect from is what was passed to the
dependencyInjectorLoader
, and subsequently, theagendaInstance
as seen in the repo's source code.The text was updated successfully, but these errors were encountered: