From 22cf61fd0923ef88a487c2fdb769be2dd71d5455 Mon Sep 17 00:00:00 2001 From: hamidbae Date: Thu, 28 Oct 2021 14:31:11 +0700 Subject: [PATCH 1/3] update new phrase to get mongo uri on from mongomemoryserver --- docs/recipes/endpoint-testing-with-mongoose.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/recipes/endpoint-testing-with-mongoose.md b/docs/recipes/endpoint-testing-with-mongoose.md index dfc222bee..7455d12b4 100644 --- a/docs/recipes/endpoint-testing-with-mongoose.md +++ b/docs/recipes/endpoint-testing-with-mongoose.md @@ -52,12 +52,12 @@ Next start the in-memory MongoDB instance and connect to Mongoose: ```js // Start MongoDB instance -const mongod = new MongoMemoryServer() +const mongod = await MongoMemoryServer.create(); // Create connection to Mongoose before tests are run test.before(async () => { - const uri = await mongod.getUri(); - await mongoose.connect(uri, {useMongoClient: true}); + const uri = mongod.getUri(); + await mongoose.connect(uri); }); ``` From af5b853c6ae1e793845878951f7682d1fb08c506 Mon Sep 17 00:00:00 2001 From: hamidbae Date: Sun, 31 Oct 2021 18:18:44 +0700 Subject: [PATCH 2/3] update mongodb instance initiation --- docs/recipes/endpoint-testing-with-mongoose.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/recipes/endpoint-testing-with-mongoose.md b/docs/recipes/endpoint-testing-with-mongoose.md index 7455d12b4..785fcb4dc 100644 --- a/docs/recipes/endpoint-testing-with-mongoose.md +++ b/docs/recipes/endpoint-testing-with-mongoose.md @@ -51,11 +51,13 @@ const User = require('../models/User'); Next start the in-memory MongoDB instance and connect to Mongoose: ```js -// Start MongoDB instance -const mongod = await MongoMemoryServer.create(); +// initiate MongoDB instance +let mongod; // Create connection to Mongoose before tests are run test.before(async () => { + // Start MongoDB instance + mongod = await MongoMemoryServer.create(); const uri = mongod.getUri(); await mongoose.connect(uri); }); From 366255ae60b95ad523bea7e1cd905802c27b28bb Mon Sep 17 00:00:00 2001 From: Mark Wubben Date: Sun, 31 Oct 2021 15:19:10 +0100 Subject: [PATCH 3/3] Use context to track the server instance, await during cleanup --- docs/recipes/endpoint-testing-with-mongoose.md | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/docs/recipes/endpoint-testing-with-mongoose.md b/docs/recipes/endpoint-testing-with-mongoose.md index 785fcb4dc..5ee0d5842 100644 --- a/docs/recipes/endpoint-testing-with-mongoose.md +++ b/docs/recipes/endpoint-testing-with-mongoose.md @@ -51,15 +51,12 @@ const User = require('../models/User'); Next start the in-memory MongoDB instance and connect to Mongoose: ```js -// initiate MongoDB instance -let mongod; - // Create connection to Mongoose before tests are run -test.before(async () => { - // Start MongoDB instance - mongod = await MongoMemoryServer.create(); - const uri = mongod.getUri(); - await mongoose.connect(uri); +test.before(async t => { + // First start MongoDB instance + t.context.mongod = await MongoMemoryServer.create(); + // And connect + await mongoose.connect(mongod.getUri()); }); ``` @@ -119,8 +116,8 @@ Finally disconnect from and stop MongoDB when all tests are done: ```js test.after.always(async () => { - mongoose.disconnect() - mongod.stop() + await mongoose.disconnect() + await t.context.mongod.stop() }) ```