diff --git a/test/examples.test.js b/test/examples.test.js index a78ddf9..a95d7b0 100644 --- a/test/examples.test.js +++ b/test/examples.test.js @@ -5,7 +5,7 @@ const mongoose = require('mongoose'); const mongooseLeanVirtuals = require('../'); describe('examples', function() { - it('attaches virtuals to result of find, findOne, findById, findByIdAndUpdate, and findOneAndUpdate if lean', function() { + it('attaches virtuals to result of find, findOne, findById, findByIdAndUpdate, and findOneAndUpdate if lean', async function() { const schema = new mongoose.Schema({ name: String }); @@ -17,34 +17,26 @@ describe('examples', function() { schema.plugin(mongooseLeanVirtuals); const Model = mongoose.model('Test', schema); - - return Model.create({ name: 'Val' }). - then(() => Promise.all([ - // You **must** pass `virtuals: true` to `lean()` - Model.find().lean({ virtuals: true }), - Model.findOne().lean({ virtuals: true }), - Model.findOneAndUpdate({}, { name: 'VAL' }).lean({ virtuals: true }) - ])). - then(results => { - const findRes = results[0]; - const findOneRes = results[1]; - const findOneAndUpdateRes = results[2]; - assert.equal(findRes[0].lowercase, 'val'); - assert.equal(findOneRes.lowercase, 'val'); - assert.equal(findOneAndUpdateRes.lowercase, 'val'); - - // Mongoose has an `id` virtual by default that gets the `_id` as a - // string. - assert.equal(findRes[0].id, findRes[0]._id.toString()); - assert.equal(findOneRes.id, findOneRes._id.toString()); - assert.equal(findOneAndUpdateRes.id, findOneAndUpdateRes._id.toString()); - }); + await Model.create({ name: 'Val' }); + + const findRes = await Model.find().lean({ virtuals: true }); + const findOneRes = await Model.findOne().lean({ virtuals: true }); + const findOneAndUpdateRes = await Model.findOneAndUpdate({}, { name: 'VAL'}).lean({ virtuals: true }); + assert.equal(findRes[0].lowercase, 'val'); + assert.equal(findOneRes.lowercase, 'val'); + assert.equal(findOneAndUpdateRes.lowercase, 'val'); + + // Mongoose has an `id` virtual by default that gets the `_id` as a + // string. + assert.equal(findRes[0].id, findRes[0]._id.toString()); + assert.equal(findOneRes.id, findOneRes._id.toString()); + assert.equal(findOneAndUpdateRes.id, findOneAndUpdateRes._id.toString()); }); /** * If you specify a list of virtuals in `lean()`, this plugin will only * apply those virtuals. This lets you pick which virtuals show up. */ - it('lets you choose which virtuals to apply', function() { + it('lets you choose which virtuals to apply', async function() { const schema = new mongoose.Schema({ name: String }, { id: false }); @@ -60,12 +52,10 @@ describe('examples', function() { const Model = mongoose.model('Test2', schema); - return Model.create({ name: 'Val' }). - then(() => Model.findOne().lean({ virtuals: ['uppercase'] })). - then(result => { - assert.equal(result.uppercase, 'VAL'); - assert.ok(!result.lowercase); - }); + await Model.create({ name: 'Val' }); + const result = await Model.findOne().lean({ virtuals: ['uppercase'] }); + assert.equal(result.uppercase, 'VAL'); + assert.ok(!result.lowercase); }); /** @@ -73,7 +63,7 @@ describe('examples', function() { * have a `parent()` method or any other Mongoose-specific functionality. * To support that use case, this plugin exports a `parent()` function that * lets you get a document's parent. */ - it('lets you access a lean subdocument\'s parent', function() { + it('lets you access a lean subdocument\'s parent', async function() { const childSchema = new mongoose.Schema({ firstName: String }); childSchema.virtual('fullName').get(function() { if (this instanceof mongoose.Document) { @@ -99,10 +89,8 @@ describe('examples', function() { lastName: 'Skywalker', child: { firstName: 'Luke' } }; - return Parent.create(doc). - then(() => Parent.findOne().lean({ virtuals: true })). - then(result => { - assert.equal(result.child.fullName, 'Luke Skywalker'); - }); + await Parent.create(doc); + const result = await Parent.findOne().lean({ virtuals: true }); + assert.equal(result.child.fullName, 'Luke Skywalker'); }); });