deno.land / x / mongoose@6.7.5 / test / docs / asyncIterator.test.js
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980'use strict';
/** * Module dependencies. */
const assert = require('assert');const start = require('../common');
const mongoose = start.mongoose;
describe('asyncIterator', function() { let db; let Movie;
before(async function() { db = await start();
const schema = new mongoose.Schema({ name: String }); await db.dropCollection('Movie').catch(() => {}); db.deleteModel(/Movie/); Movie = db.model('Movie', schema);
await Movie.create([ { name: 'Kickboxer' }, { name: 'Ip Man' }, { name: 'Enter the Dragon' } ]); });
after(async function() { await db.close(); });
function wait() { return new Promise(resolve => setTimeout(resolve, 0)); }
it('supports for/await/of on a query (gh-6737)', async function() { let names = []; for await (const doc of Movie.find().sort({ name: 1 })) { await wait(); names.push(doc.name); }
assert.deepEqual(names, ['Enter the Dragon', 'Ip Man', 'Kickboxer']); });
it('supports for/await/of on a aggregation (gh-6737)', async function() { let names = []; for await (const doc of Movie.aggregate([{ $sort: { name: -1 } }])) { await wait(); names.push(doc.name); }
assert.deepEqual(names, ['Kickboxer', 'Ip Man', 'Enter the Dragon']); });
it('supports for/await/of on a query cursor (gh-9403)', async function() { let names = []; const cursor = Movie.find().sort({ name: -1 }).cursor(); for await (const doc of cursor) { await wait(); names.push(doc.name); }
assert.deepEqual(names, ['Kickboxer', 'Ip Man', 'Enter the Dragon']); });
it('supports for/await/of on a aggregation cursor (gh-9403)', async function() { let names = []; const cursor = Movie.aggregate([{ $sort: { name: -1 } }]).cursor(); for await (const doc of cursor) { await wait(); names.push(doc.name); }
assert.deepEqual(names, ['Kickboxer', 'Ip Man', 'Enter the Dragon']); });});
Version Info