diff --git a/test/index.test.js b/test/index.test.js index e56084d..0878410 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -14,13 +14,11 @@ let baseModel; let baseObj; let baseSchema; -const createRemovableDocs = () => { - return co(function*() { - const baseDocToDelete = yield baseModel.create(baseObj); - const baseDocToRemove = yield baseModel.create(baseObj); - baseDocIdToDelete = baseDocToDelete._id; - baseDocIdToRemove = baseDocToRemove._id; - }); +const createRemovableDocs = async () => { + const baseDocToDelete = await baseModel.create(baseObj); + const baseDocToRemove = await baseModel.create(baseObj); + baseDocIdToDelete = baseDocToDelete._id; + baseDocIdToRemove = baseDocToRemove._id; }; const getDocIdBySupportedOp = (op) => { @@ -33,45 +31,43 @@ const getDocIdBySupportedOp = (op) => { } }; -before(function() { - return co(function*() { - yield mongoose.connect('mongodb://127.0.0.1:27017/mongooseLeanVirtuals'); - yield mongoose.connection.dropDatabase(); +before(async() => { + await mongoose.connect('mongodb://127.0.0.1:27017/mongooseLeanVirtuals'); + await mongoose.connection.dropDatabase(); - baseSchema = new mongoose.Schema({ - name: String, - nested: { - test: String, - objectTest: { - test: String - } + baseSchema = new mongoose.Schema({ + name: String, + nested: { + test: String, + objectTest: { + test: String } - }); - baseSchema.virtual('lowerCaseName').get(function() { - return this.name.toLowerCase(); - }); - baseSchema.virtual('nested.upperCaseTest').get(function() { - return this.nested.test.toUpperCase(); - }); - baseSchema.virtual('nested.virtualObjectTest').get(function() { - return this.nested.objectTest; - }); - baseSchema.plugin(mongooseLeanVirtuals); + } + }); + baseSchema.virtual('lowerCaseName').get(function() { + return this.name.toLowerCase(); + }); + baseSchema.virtual('nested.upperCaseTest').get(function() { + return this.nested.test.toUpperCase(); + }); + baseSchema.virtual('nested.virtualObjectTest').get(function() { + return this.nested.objectTest; + }); + baseSchema.plugin(mongooseLeanVirtuals); - baseModel = mongoose.model('baseModel', baseSchema); + baseModel = mongoose.model('baseModel', baseSchema); - baseObj = { - name: 'Val', - nested: { - test: 'Foo', - objectTest: { - test: 'Bar' - } + baseObj = { + name: 'Val', + nested: { + test: 'Foo', + objectTest: { + test: 'Bar' } - }; - const baseDoc = yield baseModel.create(baseObj); - baseDocId = baseDoc._id; - }); + } + }; + const baseDoc = await baseModel.create(baseObj); + baseDocId = baseDoc._id; }); after(function() { @@ -116,36 +112,32 @@ describe('Top level leaned virtuals work', function() { createRemovableDocs(); }); supportedOpsKeys.forEach(key => { - it(`with ${key}`, function() { - return co(function*() { - const docId = getDocIdBySupportedOp(key); - const doc = yield supportedOps[key](baseModel, docId); - assert.ok(doc); - assert.equal(doc.id, doc._id.toHexString()); - assert.equal(doc.name, 'Val'); - assert.equal(doc.lowerCaseName, 'val'); - }); - }); + it(`with ${key}`, async function() { + const docId = getDocIdBySupportedOp(key); + const doc = await supportedOps[key](baseModel, docId); + assert.ok(doc); + assert.equal(doc.id, doc._id.toHexString()); + assert.equal(doc.name, 'Val'); + assert.equal(doc.lowerCaseName, 'val'); + }); }); }); describe('Cursor', function() { - it('works (gh-21)', function() { - return co(function*() { - const schema = new mongoose.Schema({ email: String }); - schema.virtual('lower').get(function() { - return this.email.toLowerCase(); - }); - schema.plugin(mongooseLeanVirtuals); + it('works (gh-21)', async function() { + const schema = new mongoose.Schema({ email: String }); + schema.virtual('lower').get(function() { + return this.email.toLowerCase(); + }); + schema.plugin(mongooseLeanVirtuals); - const Model = mongoose.model('gh21', schema); - yield Model.create({ email: 'FOO@BAR' }); + const Model = mongoose.model('gh21', schema); + await Model.create({ email: 'FOO@BAR' }); - const cursor = Model.find().lean({ virtuals: true }).cursor(); - const doc = yield cursor.next(); - assert.equal(doc.email, 'FOO@BAR'); - assert.equal(doc.lower, 'foo@bar'); - }); + const cursor = Model.find().lean({ virtuals: true }).cursor(); + const doc = await cursor.next(); + assert.equal(doc.email, 'FOO@BAR'); + assert.equal(doc.lower, 'foo@bar'); }); }); @@ -154,16 +146,14 @@ describe('Nested virtuals work', function() { createRemovableDocs(); }); supportedOpsKeys.forEach(key => { - it(`with ${key}`, function() { - return co(function*() { - const docId = getDocIdBySupportedOp(key); - const doc = yield supportedOps[key](baseModel, docId); - assert.ok(doc); - assert.equal(doc.nested.test, 'Foo'); - assert.equal(doc.nested.upperCaseTest, 'FOO'); - assert.equal(doc.nested.objectTest.test, 'Bar'); - assert.equal(doc.nested.virtualObjectTest.test, 'Bar'); - }); + it(`with ${key}`, async function() { + const docId = getDocIdBySupportedOp(key); + const doc = await supportedOps[key](baseModel, docId); + assert.ok(doc); + assert.equal(doc.nested.test, 'Foo'); + assert.equal(doc.nested.upperCaseTest, 'FOO'); + assert.equal(doc.nested.objectTest.test, 'Bar'); + assert.equal(doc.nested.virtualObjectTest.test, 'Bar'); }); }); }); @@ -173,14 +163,12 @@ describe('Nested object virtuals work (gh-43)', function() { createRemovableDocs(); }); supportedOpsKeys.forEach(key => { - it(`with ${key}`, function() { - return co(function*() { - const docId = getDocIdBySupportedOp(key); - const doc = yield supportedOps[key](baseModel, docId); - assert.ok(doc); - assert.equal(doc.nested.objectTest.test, 'Bar'); - assert.equal(doc.nested.virtualObjectTest.test, 'Bar'); - }); + it(`with ${key}`, async function() { + const docId = getDocIdBySupportedOp(key); + const doc = await supportedOps[key](baseModel, docId); + assert.ok(doc); + assert.equal(doc.nested.objectTest.test, 'Bar'); + assert.equal(doc.nested.virtualObjectTest.test, 'Bar'); }); }); }); @@ -202,60 +190,56 @@ describe('Nested schema virtuals work', function() { let parentDocIdToDelete; let parentModel; - before(function() { - return co(function*() { - const parentSchema = new mongoose.Schema({ - nested: baseSchema, - arr: [baseSchema] - }); - parentSchema.plugin(mongooseLeanVirtuals); - parentModel = mongoose.model('parentModel', parentSchema); + before(async () => { + const parentSchema = new mongoose.Schema({ + nested: baseSchema, + arr: [baseSchema] + }); + parentSchema.plugin(mongooseLeanVirtuals); + parentModel = mongoose.model('parentModel', parentSchema); - const parentDoc = yield parentModel.create({ - nested: baseObj, - arr: [baseObj, baseObj], - }); - const parentDocToRemove = yield parentModel.create({ - nested: baseObj, - arr: [baseObj, baseObj], - }); - const parentDocToDelete = yield parentModel.create({ - nested: baseObj, - arr: [baseObj, baseObj], - }); - parentBaseDocId = parentDoc._id; - parentDocIdToRemove = parentDocToRemove._id; - parentDocIdToDelete = parentDocToDelete._id; + const parentDoc = await parentModel.create({ + nested: baseObj, + arr: [baseObj, baseObj], + }); + const parentDocToRemove = await parentModel.create({ + nested: baseObj, + arr: [baseObj, baseObj], + }); + const parentDocToDelete = await parentModel.create({ + nested: baseObj, + arr: [baseObj, baseObj], }); + parentBaseDocId = parentDoc._id; + parentDocIdToRemove = parentDocToRemove._id; + parentDocIdToDelete = parentDocToDelete._id; }); supportedOpsKeys.forEach(key => { - it(`with ${key}`, function() { - return co(function*() { - let parentDocId = parentBaseDocId; - if (key === 'findOneAndRemove') { - parentDocId = parentDocIdToRemove; - } - if (key === 'findOneAndDelete') { - parentDocId = parentDocIdToDelete; - } - const doc = yield supportedOps[key](parentModel, parentDocId); - assert.ok(doc); - assert.equal(doc.nested.name, 'Val'); - assert.equal(doc.nested.lowerCaseName, 'val'); - assert.equal(doc.nested.nested.test, 'Foo'); - assert.equal(doc.nested.nested.upperCaseTest, 'FOO'); - doc.arr.forEach((doc) => { - assert.equal(doc.name, 'Val'); - assert.equal(doc.lowerCaseName, 'val'); - assert.equal(doc.nested.test, 'Foo'); - assert.equal(doc.nested.upperCaseTest, 'FOO'); - }); + it(`with ${key}`, async function() { + let parentDocId = parentBaseDocId; + if (key === 'findOneAndRemove') { + parentDocId = parentDocIdToRemove; + } + if (key === 'findOneAndDelete') { + parentDocId = parentDocIdToDelete; + } + const doc = await supportedOps[key](parentModel, parentDocId); + assert.ok(doc); + assert.equal(doc.nested.name, 'Val'); + assert.equal(doc.nested.lowerCaseName, 'val'); + assert.equal(doc.nested.nested.test, 'Foo'); + assert.equal(doc.nested.nested.upperCaseTest, 'FOO'); + doc.arr.forEach((doc) => { + assert.equal(doc.name, 'Val'); + assert.equal(doc.lowerCaseName, 'val'); + assert.equal(doc.nested.test, 'Foo'); + assert.equal(doc.nested.upperCaseTest, 'FOO'); }); }); }); - it('with nested schemas (gh-20)', function() { + it('with nested schemas (gh-20)', async function() { const schema = new mongoose.Schema({ name: String }); schema.virtual('lower').get(function() { return this.name.toLowerCase(); @@ -268,22 +252,19 @@ describe('Nested schema virtuals work', function() { parentSchema.plugin(mongooseLeanVirtuals); const Model = mongoose.model('gh20', parentSchema); + await Model.create({ + nested: { name: 'FOO' }, + arr: [{ name: 'BAR' }, { name: 'BAZ' }] + }); - return co(function*() { - yield Model.create({ - nested: { name: 'FOO' }, - arr: [{ name: 'BAR' }, { name: 'BAZ' }] - }); - - const doc = yield Model.findOne().lean({ virtuals: true }); + const doc = await Model.findOne().lean({ virtuals: true }); - assert.equal(doc.nested.lower, 'foo'); - assert.equal(doc.arr[0].lower, 'bar'); - assert.equal(doc.arr[1].lower, 'baz'); - }); + assert.equal(doc.nested.lower, 'foo'); + assert.equal(doc.arr[0].lower, 'bar'); + assert.equal(doc.arr[1].lower, 'baz'); }); - it('can access parent doc (gh-40) (gh-41) (gh-51)', function() { + it('can access parent doc (gh-40) (gh-41) (gh-51)', async function() { const childSchema = new mongoose.Schema({ firstName: String }); childSchema.virtual('fullName').get(function() { if (this instanceof mongoose.Document) { @@ -302,50 +283,48 @@ describe('Nested schema virtuals work', function() { const Model = mongoose.model('gh40', parentSchema); - return co(function*() { - let doc = yield Model.create({ - firstName: 'Anakin', - lastName: 'Skywalker', - child: { firstName: 'Luke' }, - children: [{ firstName: 'Luke' }, null] - }); - - yield Model.collection.updateOne({ _id: doc._id }, { $push: { children: 42 } }); + let doc = await Model.create({ + firstName: 'Anakin', + lastName: 'Skywalker', + child: { firstName: 'Luke' }, + children: [{ firstName: 'Luke' }, null] + }); - // Would error out in v0.7.2 because of parent tracking and the '42' element - doc = yield Model.findOne().lean({ virtuals: true }); + await Model.collection.updateOne({ _id: doc._id }, { $push: { children: 42 } }); - assert.equal(doc.child.fullName, 'Luke Skywalker'); - assert.equal(doc.children[0].fullName, 'Luke Skywalker'); - assert.equal(doc.children[1], null); - assert.equal(doc.children[2], 42); + // Would error out in v0.7.2 because of parent tracking and the '42' element + doc = await Model.findOne().lean({ virtuals: true }); - doc = yield Model.find().lean({ virtuals: true }).then(res => res[0]); + assert.equal(doc.child.fullName, 'Luke Skywalker'); + assert.equal(doc.children[0].fullName, 'Luke Skywalker'); + assert.equal(doc.children[1], null); + assert.equal(doc.children[2], 42); - assert.equal(doc.child.fullName, 'Luke Skywalker'); - assert.equal(doc.children[0].fullName, 'Luke Skywalker'); - assert.equal(doc.children[1], null); - assert.equal(doc.children[2], 42); + doc = await Model.find().lean({ virtuals: true }).then(res => res[0]); - yield Model.create({ - firstName: 'Han', - lastName: 'Solo', - child: { firstName: 'Anakin' }, - children: [{ firstName: 'Anakin' }, null] - }); - const docs = yield Model.find().sort({ lastName: -1 }).lean({ virtuals: true }); - assert.equal(docs[0].child.fullName, 'Anakin Solo'); - assert.equal(docs[0].children[0].fullName, 'Anakin Solo'); - assert.equal(docs[0].children[1], null); + assert.equal(doc.child.fullName, 'Luke Skywalker'); + assert.equal(doc.children[0].fullName, 'Luke Skywalker'); + assert.equal(doc.children[1], null); + assert.equal(doc.children[2], 42); - assert.equal(docs[1].child.fullName, 'Luke Skywalker'); - assert.equal(docs[1].children[0].fullName, 'Luke Skywalker'); - assert.equal(docs[1].children[1], null); - assert.equal(docs[1].children[2], 42); + await Model.create({ + firstName: 'Han', + lastName: 'Solo', + child: { firstName: 'Anakin' }, + children: [{ firstName: 'Anakin' }, null] }); + const docs = await Model.find().sort({ lastName: -1 }).lean({ virtuals: true }); + assert.equal(docs[0].child.fullName, 'Anakin Solo'); + assert.equal(docs[0].children[0].fullName, 'Anakin Solo'); + assert.equal(docs[0].children[1], null); + + assert.equal(docs[1].child.fullName, 'Luke Skywalker'); + assert.equal(docs[1].children[0].fullName, 'Luke Skywalker'); + assert.equal(docs[1].children[1], null); + assert.equal(docs[1].children[2], 42); }); - it('child schemas (gh-28)', function() { + it('child schemas (gh-28)', async function() { const barSchema = Schema({ number: Number }); barSchema.plugin(mongooseLeanVirtuals); barSchema.virtual('doubleNumber').get(function() { @@ -360,16 +339,14 @@ describe('Nested schema virtuals work', function() { }); const Foo = mongoose.model('gh28_foo', fooSchema); - return co(function*() { - yield Foo.create({ bars: [{ number: 1 }, { number: 2 }] }); + await Foo.create({ bars: [{ number: 1 }, { number: 2 }] }); - const doc = yield Foo.findOne().lean({ virtuals: true }); - assert.equal(doc.barDoubleTotal, 6); - }); + const doc = await Foo.findOne().lean({ virtuals: true }); + assert.equal(doc.barDoubleTotal, 6); }); }); -it('works with recursive schemas (gh-33)', function() { +it('works with recursive schemas (gh-33)', async function() { const postSchema = Schema({ title: String, content: String @@ -385,21 +362,18 @@ it('works with recursive schemas (gh-33)', function() { postSchema.plugin(mongooseLeanVirtuals); const Post = mongoose.model('gh33_Post', postSchema); - - return co(function*() { - yield Post.create({ - title: 'Test', - content: 'This is a test', - comments: [{ content: 'It works!' }] - }); - - const doc = yield Post.findOne().lean({ virtuals: true }); - assert.equal(doc.comments.length, 1); - assert.equal(doc.comments[0].answer, 42); + await Post.create({ + title: 'Test', + content: 'This is a test', + comments: [{ content: 'It works!' }] }); + + const doc = await Post.findOne().lean({ virtuals: true }); + assert.equal(doc.comments.length, 1); + assert.equal(doc.comments[0].answer, 42); }); -it('applies virtuals in doubly nested arrays (gh-38)', function() { +it('applies virtuals in doubly nested arrays (gh-38)', async function() { const subArraySchema = Schema({ name: String }); subArraySchema.virtual('lowercase').get(function() { return this.name.toLowerCase(); @@ -409,96 +383,74 @@ it('applies virtuals in doubly nested arrays (gh-38)', function() { const testSchema = Schema({ title: String, array: [arraySchema] }); testSchema.plugin(mongooseLeanVirtuals); const Model = mongoose.model('gh38', testSchema); - - return co(function*() { - yield Model.create({ - title: 'test', - array: [{ - subArray: [{ - name: 'TEST', - }] + await Model.create({ + title: 'test', + array: [{ + subArray: [{ + name: 'TEST', }] - }); - - const testDoc = yield Model.findOne({ title: 'test' }).lean({ virtuals: true }); - const subObject = testDoc.array[0].subArray[0]; - assert.equal(subObject.name, 'TEST'); - assert.equal(subObject.lowercase, 'test'); + }] }); + + const testDoc = await Model.findOne({ title: 'test' }).lean({ virtuals: true }); + const subObject = testDoc.array[0].subArray[0]; + assert.equal(subObject.name, 'TEST'); + assert.equal(subObject.lowercase, 'test'); }); -it('skips non-existent virtuals if specifying a list of virtuals (gh-42)', function() { +it('skips non-existent virtuals if specifying a list of virtuals (gh-42)', async function() { const testSchema = Schema({ title: String }); testSchema.virtual('test').get(() => 42); testSchema.plugin(mongooseLeanVirtuals); const Model = mongoose.model('gh42', testSchema); - return co(function*() { - yield Model.create({ title: 'test' }); + await Model.create({ title: 'test' }); - const testDoc = yield Model.findOne({ title: 'test' }).lean({ virtuals: ['test', 'doesntexist'] }); - assert.equal(testDoc.test, 42); - assert.strictEqual(testDoc.doesntexist, void 0); - }); + const testDoc = await Model.findOne({ title: 'test' }).lean({ virtuals: ['test', 'doesntexist'] }); + assert.equal(testDoc.test, 42); + assert.strictEqual(testDoc.doesntexist, void 0); }); describe('Discriminators work', () => { let childDocId; let childModel; - before(function() { - return co(function*() { - const childSchema = new mongoose.Schema({ - name: String, - nested2: { - test: String - } - }); - childSchema.virtual('upperCaseName').get(function() { - return this.name.toUpperCase(); - }); - childSchema.virtual('nested.lowerCaseTest').get(function() { - return this.nested.test.toLowerCase(); - }); + before(async () => { + const childSchema = new mongoose.Schema({ + name: String, + nested2: { + test: String + } + }); + childSchema.virtual('upperCaseName').get(function() { + return this.name.toUpperCase(); + }); + childSchema.virtual('nested.lowerCaseTest').get(function() { + return this.nested.test.toLowerCase(); + }); - childModel = baseModel.discriminator('childModel', childSchema); + childModel = baseModel.discriminator('childModel', childSchema); - const childDoc = yield childModel.create({ - name: 'Val', - nested: { - test: 'Foo', - } - }); - childDocId = childDoc._id; - - yield childModel.create({ - name: 'Val', - nested: { - test: 'Foo', - } - }); + const childDoc = await childModel.create({ + name: 'Val', + nested: { + test: 'Foo', + } }); - }); + childDocId = childDoc._id; - it('with find', function() { - return co(function*() { - const docs = yield childModel.find(); - assert.ok(docs); - docs.forEach((doc) => { - assert.equal(doc.name, 'Val'); - assert.equal(doc.lowerCaseName, 'val'); - assert.equal(doc.upperCaseName, 'VAL'); - assert.equal(doc.nested.test, 'Foo'); - assert.equal(doc.nested.upperCaseTest, 'FOO'); - assert.equal(doc.nested.lowerCaseTest, 'foo'); - }); + await childModel.create({ + name: 'Val', + nested: { + test: 'Foo', + } }); }); - it('with findOne', () => { - return co(function*() { - const doc = yield supportedOps.find(childModel, childDocId); - assert.ok(doc); + it('with find', async function() { + const docs = await childModel.find(); + assert.ok(docs); + docs.forEach((doc) => { assert.equal(doc.name, 'Val'); assert.equal(doc.lowerCaseName, 'val'); assert.equal(doc.upperCaseName, 'VAL'); @@ -508,7 +460,18 @@ describe('Discriminators work', () => { }); }); - it('correctly applies getters on virtuals in lean queries', function() { + it('with findOne', async () => { + const doc = await supportedOps.find(childModel, childDocId); + assert.ok(doc); + assert.equal(doc.name, 'Val'); + assert.equal(doc.lowerCaseName, 'val'); + assert.equal(doc.upperCaseName, 'VAL'); + assert.equal(doc.nested.test, 'Foo'); + assert.equal(doc.nested.upperCaseTest, 'FOO'); + assert.equal(doc.nested.lowerCaseTest, 'foo'); + }); + + it('correctly applies getters on virtuals in lean queries', async function() { let getterCalled = false; const childSchema = new mongoose.Schema({ name: String, parentId: 'ObjectId' }); const Child = mongoose.model('C', childSchema); @@ -525,17 +488,15 @@ describe('Discriminators work', () => { }); parentSchema.plugin(mongooseLeanVirtuals); const Parent = mongoose.model('P', parentSchema); - - return Parent.create({ name: 'Darth Vader' }) - .then(p => Child.create({ name: 'Luke Skywalker', parentId: p })) - .then(() => Parent.findOne().populate('children').lean({ virtuals: true })) - .then(doc => { - assert.ok(getterCalled); - assert.ok(doc.children); - }); + // spoiler alert + const parent = await Parent.create({ name: 'Darth Vader' }); + await Child.create({ name: 'Luke Skywalker', parentId: parent }) + const doc = await Parent.findOne().populate('children').lean({ virtuals: true }); + assert.ok(getterCalled); + assert.ok(doc.children) }); - it('lets you choose which virtuals to apply in the root schema (gh-34)', function() { + it('lets you choose which virtuals to apply in the root schema (gh-34)', async function() { const schema = new mongoose.Schema({ name: String, childs: [{ other: String }], @@ -549,14 +510,12 @@ describe('Discriminators work', () => { const Model = mongoose.model('gh34a', schema); - return Model.create({ name: 'Val', childs: [{ other: 'val' }] }). - then(() => Model.findOne().lean({ virtuals: ['lowercaseName'] })). - then(result => { - assert.equal(result.lowercaseName, 'val'); - }); + await Model.create({ name: 'Val', childs: [{ other: 'val' }] }); + const result = await Model.findOne().lean({ virtuals: ['lowercaseName'] }); + assert.equal(result.lowercaseName, 'val'); }); - it('lets you choose which virtuals to apply in the nested schema (gh-34)', function() { + it('lets you choose which virtuals to apply in the nested schema (gh-34)', async function() { const subschema = new mongoose.Schema({ other: String, }, { id: false }); @@ -574,14 +533,12 @@ describe('Discriminators work', () => { const Model = mongoose.model('gh34b', schema); - return Model.create({ name: 'Val', childs: [{ other: 'val' }] }). - then(() => Model.findOne().lean({ virtuals: ['childs.uppercaseOther'] })). - then(result => { - assert.equal(result.childs[0].uppercaseOther, 'VAL'); - }); + await Model.create({ name: 'Val', childs: [{ other: 'val' }] }); + const result = await Model.findOne().lean({ virtuals: ['childs.uppercaseOther'] }); + assert.equal(result.childs[0].uppercaseOther, 'VAL'); }); - it('lets you choose which virtuals to apply in the root and nested (gh-34)', function() { + it('lets you choose which virtuals to apply in the root and nested (gh-34)', async function() { const subschema = new mongoose.Schema({ other: String, }, { id: false }); @@ -603,15 +560,13 @@ describe('Discriminators work', () => { const Model = mongoose.model('gh34c', schema); - return Model.create({ name: 'Val', childs: [{ other: 'val' }] }). - then(() => Model.findOne().lean({ virtuals: ['lowercaseName', 'childs.uppercaseOther'] })). - then(result => { - assert.equal(result.lowercaseName, 'val'); - assert.equal(result.childs[0].uppercaseOther, 'VAL'); - }); + await Model.create({ name: 'Val', childs: [{ other: 'val' }] }); + const result = await Model.findOne().lean({ virtuals: ['lowercaseName', 'childs.uppercaseOther' ] }); + assert.equal(result.lowercaseName, 'val'); + assert.equal(result.childs[0].uppercaseOther, 'VAL'); }); - it('nested virtuals that are objects return the value (gh-43)', function() { + it('nested virtuals that are objects return the value (gh-43)', async function() { const schema = new mongoose.Schema({ nested: { test: { @@ -628,15 +583,13 @@ describe('Discriminators work', () => { const Model = mongoose.model('gh43a', schema); - return Model.create({ nested: { test: { a: 'Val' } } }). - then(() => Model.findOne().lean({ virtuals: ['nested.test2'] })). - then(result => { - assert.equal(result.nested.test.a, 'Val'); - assert.equal(result.nested.test2.a, 'Val'); - }); + await Model.create({ nested: { test: { a: 'Val' } } }); + const result = await Model.findOne().lean({ virtuals: ['nested.test2'] }); + assert.equal(result.nested.test.a, 'Val'); + assert.equal(result.nested.test2.a, 'Val'); }); - it('nested virtuals that are objects return the value that also have child schemas (gh-43)', function() { + it('nested virtuals that are objects return the value that also have child schemas (gh-43)', async function() { const subschema = new mongoose.Schema({ other: String, }, { id: false }); @@ -662,15 +615,13 @@ describe('Discriminators work', () => { const Model = mongoose.model('gh43b', schema); - return Model.create({ childs: [{ other: 'Val' }], nested: { test: { a: 'Val' } } }). - then(() => Model.findOne().lean({ virtuals: ['nested.test2'] })). - then(result => { - assert.equal(result.nested.test.a, 'Val'); - assert.equal(result.nested.test2.a, 'Val'); - }); + await Model.create({ childs: [{ other: 'Val'}], nested: { test: { a: 'Val' } } }); + const result = await Model.findOne().lean({ virtuals: ['nested.test2'] }); + assert.equal(result.nested.test.a, 'Val'); + assert.equal(result.nested.test2.a, 'Val'); }); - it('sets empty array if no result and justOne: false', function() { + it('sets empty array if no result and justOne: false', async function() { const childSchema = new mongoose.Schema({ name: String, parentId: 'ObjectId' }); const Child = mongoose.model('C2', childSchema); @@ -684,12 +635,12 @@ describe('Discriminators work', () => { parentSchema.plugin(mongooseLeanVirtuals); const Parent = mongoose.model('P2', parentSchema); - return Parent.create({ name: 'Darth Vader' }) - .then(() => Parent.findOne().populate('children').lean({ virtuals: true })) - .then(res => assert.deepEqual(res.children, [])); + await Parent.create({ name: 'Darth Vader' }); + const res = await Parent.findOne().populate('children').lean({ virtuals: true }); + assert.deepEqual(res.children, []) }); - it('returns the same nested and referenced virtuals whether all virtuals selected or each specifically selected', function() { + it('returns the same nested and referenced virtuals whether all virtuals selected or each specifically selected', async function() { let childGetterCalled = false; let parentGetterCalled = false; let surnameGetterCalled = false; @@ -736,52 +687,48 @@ describe('Discriminators work', () => { parentSchema.plugin(mongooseLeanVirtuals); const Parent = mongoose.model('Parent2', parentSchema); - return Child.create({ name: 'Luke', surname: {name: 'Skywalker'} }) - .then(c => Parent.create({ role: 'Father', surname: {name: 'Vader'}, allegiance: {name: 'Empire'}, child: c })) - .then(() => Parent.findOne().populate('child').lean({ virtuals: true })) - .then(doc => { - assert.ok(childGetterCalled); - assert.ok(parentGetterCalled); - assert.ok(surnameGetterCalled); - assert.ok(allegianceGetterCalled); - assert.equal(doc.role, 'Father'); - assert.equal(doc.uppercaseRole, 'FATHER'); - assert.equal(doc.surname.name, 'Vader'); - assert.equal(doc.surname.uppercaseSurname, 'VADER'); - assert.equal(doc.allegiance.name, 'Empire'); - assert.equal(doc.allegiance.uppercaseAllegiance, 'EMPIRE'); - assert.equal(doc.child.name, 'Luke'); - assert.equal(doc.child.uppercaseName, 'LUKE'); - assert.equal(doc.child.surname.name, 'Skywalker'); - assert.equal(doc.child.surname.uppercaseSurname, 'SKYWALKER'); - // reset getter checks - childGetterCalled = false; - parentGetterCalled = false; - surnameGetterCalled = false; - allegianceGetterCalled = false; - }) - .then(() => Parent.findOne().populate('child').lean({ - virtuals: ['uppercaseRole', 'surname.uppercaseSurname', 'allegiance.uppercaseAllegiance', 'child.uppercaseName', 'child.surname.uppercaseSurname'] - })) - .then(doc => { - assert.ok(childGetterCalled); - assert.ok(parentGetterCalled); - assert.ok(surnameGetterCalled); - assert.ok(allegianceGetterCalled); - assert.equal(doc.role, 'Father'); - assert.equal(doc.uppercaseRole, 'FATHER'); - assert.equal(doc.surname.name, 'Vader'); - assert.equal(doc.surname.uppercaseSurname, 'VADER'); - assert.equal(doc.allegiance.name, 'Empire'); - assert.equal(doc.allegiance.uppercaseAllegiance, 'EMPIRE'); - assert.equal(doc.child.name, 'Luke'); - assert.equal(doc.child.uppercaseName, 'LUKE'); - assert.equal(doc.child.surname.name, 'Skywalker'); - assert.equal(doc.child.surname.uppercaseSurname, 'SKYWALKER'); - }); + const child = await Child.create({ name: 'Luke', surname: { name: 'Skywalker' } }); + const parent = await Parent.create({ role: 'Father', surname: { name: 'Vader' }, allegiance: { name: 'Empire' }, child: child }); + let doc = await Parent.findOne().populate('child').lean({ virtuals: true }); + assert.ok(childGetterCalled); + assert.ok(parentGetterCalled); + assert.ok(surnameGetterCalled); + assert.ok(allegianceGetterCalled); + assert.equal(doc.role, 'Father'); + assert.equal(doc.uppercaseRole, 'FATHER'); + assert.equal(doc.surname.name, 'Vader'); + assert.equal(doc.surname.uppercaseSurname, 'VADER'); + assert.equal(doc.allegiance.name, 'Empire'); + assert.equal(doc.allegiance.uppercaseAllegiance, 'EMPIRE'); + assert.equal(doc.child.name, 'Luke'); + assert.equal(doc.child.uppercaseName, 'LUKE'); + assert.equal(doc.child.surname.name, 'Skywalker'); + assert.equal(doc.child.surname.uppercaseSurname, 'SKYWALKER'); + // reset getter checks + childGetterCalled = false; + parentGetterCalled = false; + surnameGetterCalled = false; + allegianceGetterCalled = false; + doc = await Parent.findOne().populate('child').lean({ + virtuals: ['uppercaseRole', 'surname.uppercaseSurname', 'allegiance.uppercaseAllegiance', 'child.uppercaseName', 'child.surname.uppercaseSurname'] + }); + assert.ok(childGetterCalled); + assert.ok(parentGetterCalled); + assert.ok(surnameGetterCalled); + assert.ok(allegianceGetterCalled); + assert.equal(doc.role, 'Father'); + assert.equal(doc.uppercaseRole, 'FATHER'); + assert.equal(doc.surname.name, 'Vader'); + assert.equal(doc.surname.uppercaseSurname, 'VADER'); + assert.equal(doc.allegiance.name, 'Empire'); + assert.equal(doc.allegiance.uppercaseAllegiance, 'EMPIRE'); + assert.equal(doc.child.name, 'Luke'); + assert.equal(doc.child.uppercaseName, 'LUKE'); + assert.equal(doc.child.surname.name, 'Skywalker'); + assert.equal(doc.child.surname.uppercaseSurname, 'SKYWALKER'); }); describe('enabledByDefault', function() { - it('should attach virtuals if enabledByDefault is set', function() { + it('should attach virtuals if enabledByDefault is set', async function() { const testSchema = new mongoose.Schema({ name: String }); @@ -792,9 +739,9 @@ describe('Discriminators work', () => { testSchema.plugin(mongooseLeanVirtuals, { enabledByDefault: true }); const Test = mongoose.model('gh52', testSchema); - return Test.create({ name: 'TEST TESTERSON' }).then(() => Test.findOne().lean()).then(doc => { - assert.equal(doc.lowercase, 'test testerson'); - }); + await Test.create({ name: 'TEST TESTERSON' }); + const doc = await Test.findOne().lean(); + assert.equal(doc.lowercase, 'test testerson'); }); }); });