From 3a0d10216a9ec308b636d8b6e9d22da4a0bbfd18 Mon Sep 17 00:00:00 2001 From: Hamid Baehaqi Date: Sun, 31 Oct 2021 21:20:00 +0700 Subject: [PATCH] Update Mongoose recipe Co-authored-by: Mark Wubben --- docs/recipes/endpoint-testing-with-mongoose.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/docs/recipes/endpoint-testing-with-mongoose.md b/docs/recipes/endpoint-testing-with-mongoose.md index dfc222bee..5ee0d5842 100644 --- a/docs/recipes/endpoint-testing-with-mongoose.md +++ b/docs/recipes/endpoint-testing-with-mongoose.md @@ -51,13 +51,12 @@ const User = require('../models/User'); Next start the in-memory MongoDB instance and connect to Mongoose: ```js -// Start MongoDB instance -const mongod = new MongoMemoryServer() - // Create connection to Mongoose before tests are run -test.before(async () => { - const uri = await mongod.getUri(); - await mongoose.connect(uri, {useMongoClient: true}); +test.before(async t => { + // First start MongoDB instance + t.context.mongod = await MongoMemoryServer.create(); + // And connect + await mongoose.connect(mongod.getUri()); }); ``` @@ -117,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() }) ```