Update Mongoose recipe

Co-authored-by: Mark Wubben <mark@novemberborn.net>
This commit is contained in:
Hamid Baehaqi 2021-10-31 21:20:00 +07:00 committed by GitHub
parent 7098eece42
commit 3a0d10216a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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()
})
```