From 9bb4b034ab4ca3cb03cf18eb888bdd308612d2d5 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 28 Sep 2019 08:26:02 -0700 Subject: [PATCH] refactor: remove async as a prod dependency Fix #8073 --- package.json | 2 +- test/docs/discriminators.test.js | 33 ++-------- test/model.discriminator.querying.test.js | 78 ++++++++--------------- test/model.populate.test.js | 68 ++++++-------------- test/query.test.js | 1 + 5 files changed, 52 insertions(+), 130 deletions(-) diff --git a/package.json b/package.json index f64ca2add80..8cb9ed2b848 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,6 @@ ], "license": "MIT", "dependencies": { - "async": "2.6.2", "bson": "~1.1.1", "kareem": "2.3.1", "mongodb": "3.3.2", @@ -37,6 +36,7 @@ "acquit": "1.x", "acquit-ignore": "0.1.x", "acquit-require": "0.1.x", + "async": "2.6.2", "babel-loader": "7.1.4", "babel-preset-es2015": "6.24.1", "benchmark": "2.1.4", diff --git a/test/docs/discriminators.test.js b/test/docs/discriminators.test.js index 76fb52b394a..829976dbf82 100644 --- a/test/docs/discriminators.test.js +++ b/test/docs/discriminators.test.js @@ -1,7 +1,6 @@ 'use strict'; var assert = require('assert'); -var async = require('async'); var mongoose = require('../../'); var Schema = mongoose.Schema; @@ -90,21 +89,14 @@ describe('discriminator docs', function () { }); }; - async.map([event1, event2, event3], save, function (error) { - // acquit:ignore:start - assert.ifError(error); - // acquit:ignore:end - - Event.countDocuments({}, function (error, count) { - // acquit:ignore:start - assert.ifError(error); - // acquit:ignore:end + Promise.all([event1.save(), event2.save(), event3.save()]). + then(() => Event.countDocuments()). + then(count => { assert.equal(count, 3); // acquit:ignore:start done(); // acquit:ignore:end }); - }); }); /** @@ -138,21 +130,9 @@ describe('discriminator docs', function () { var event2 = new ClickedLinkEvent({time: Date.now(), url: 'google.com'}); var event3 = new SignedUpEvent({time: Date.now(), user: 'testuser'}); - var save = function (doc, callback) { - doc.save(function (error, doc) { - callback(error, doc); - }); - }; - - async.map([event1, event2, event3], save, function (error) { - // acquit:ignore:start - assert.ifError(error); - // acquit:ignore:end - - ClickedLinkEvent.find({}, function (error, docs) { - // acquit:ignore:start - assert.ifError(error); - // acquit:ignore:end + Promise.all([event1.save(), event2.save(), event3.save()]). + then(() => ClickedLinkEvent.find({})). + then(docs => { assert.equal(docs.length, 1); assert.equal(docs[0]._id.toString(), event2._id.toString()); assert.equal(docs[0].url, 'google.com'); @@ -160,7 +140,6 @@ describe('discriminator docs', function () { done(); // acquit:ignore:end }); - }); }); /** diff --git a/test/model.discriminator.querying.test.js b/test/model.discriminator.querying.test.js index 4d780f69499..9132cc78e04 100644 --- a/test/model.discriminator.querying.test.js +++ b/test/model.discriminator.querying.test.js @@ -10,8 +10,6 @@ const Schema = mongoose.Schema; const assert = require('assert'); const random = require('../lib/utils').random; const util = require('util'); -const async = require('async'); - /** * Setup @@ -47,21 +45,10 @@ describe('model', function() { SecretEvent = BaseEvent.discriminator('model-discriminator-querying-secret', SecretEventSchema); }); - afterEach(function(done) { - async.series( - [ - function removeBaseEvent(next) { - BaseEvent.deleteMany({}, next); - }, - function removeImpressionEvent(next) { - ImpressionEvent.deleteMany({}, next); - }, - function removeConversionEvent(next) { - ConversionEvent.deleteMany({}, next); - } - ], - done - ); + afterEach(function() { + return BaseEvent.deleteMany({}). + then(() => ImpressionEvent.deleteMany({})). + then(() => ConversionEvent.deleteMany({})); }); after(function(done) { @@ -86,7 +73,7 @@ describe('model', function() { ContainerModel = db.model('container-event-model', ContainerSchema); }); - it('into non-discriminated arrays works', function(done) { + it('into non-discriminated arrays works', function() { const c = new ContainerModel({ title: 'events-group-1' }); @@ -95,32 +82,25 @@ describe('model', function() { const d3 = new DiscCustomEvent(); c.events.push(d1); c.events.push(d2); - async.series( - [ - function(next) { d1.save(next); }, - function(next) { d2.save(next); }, - function(next) { d3.save(next); }, - function(next) { c.save(next); }, - function(next) { - ContainerModel.findOne({}).populate('events').exec(function(err, doc) { - assert.ifError(err); - assert.ok(doc.events && doc.events.length); - assert.equal(doc.events.length, 2); - doc.events.push(d3); - let hasDisc = false; - const discKey = DiscCustomEvent.schema.discriminatorMapping.key; - doc.events.forEach(function(subDoc) { - if (discKey in subDoc) { - hasDisc = true; - } - }); - assert.ok(hasDisc); - next(); - }); - } - ], - done - ); + + return d1.save(). + then(() => d2.save()). + then(() => d3.save()). + then(() => c.save()). + then(() => ContainerModel.findOne({}).populate('events')). + then(doc => { + assert.ok(doc.events && doc.events.length); + assert.equal(doc.events.length, 2); + doc.events.push(d3); + let hasDisc = false; + const discKey = DiscCustomEvent.schema.discriminatorMapping.key; + doc.events.forEach(function(subDoc) { + if (discKey in subDoc) { + hasDisc = true; + } + }); + assert.ok(hasDisc); + }); }); }); @@ -879,18 +859,12 @@ describe('model', function() { describe('aggregate', function() { let impressionEvent, conversionEvent, ignoredImpressionEvent; - beforeEach(function(done) { + beforeEach(function() { impressionEvent = new ImpressionEvent({name: 'Test Event'}); conversionEvent = new ConversionEvent({name: 'Test Event', revenue: 10}); ignoredImpressionEvent = new ImpressionEvent({name: 'Ignored Event'}); - async.forEach( - [impressionEvent, conversionEvent, ignoredImpressionEvent], - function(doc, cb) { - doc.save(cb); - }, - done - ); + return Promise.all([impressionEvent, conversionEvent, ignoredImpressionEvent].map(d => d.save())); }); describe('using "RootModel#aggregate"', function() { diff --git a/test/model.populate.test.js b/test/model.populate.test.js index 51c95ca766c..efc2be74ba2 100644 --- a/test/model.populate.test.js +++ b/test/model.populate.test.js @@ -5,7 +5,6 @@ */ const assert = require('assert'); -const async = require('async'); const co = require('co'); const start = require('./common'); const utils = require('../lib/utils'); @@ -4035,7 +4034,7 @@ describe('model: populate:', function() { }); }); - it('out-of-order discriminators (gh-4073)', function(done) { + it('out-of-order discriminators (gh-4073)', function() { const UserSchema = new Schema({ name: String }); @@ -4094,54 +4093,23 @@ describe('model: populate:', function() { const be2 = new BlogPostEvent({ blogpost: b2 }); const be3 = new BlogPostEvent({ blogpost: b3 }); - async.series( - [ - u1.save.bind(u1), - u2.save.bind(u2), - u3.save.bind(u3), - - c1.save.bind(c1), - c2.save.bind(c2), - c3.save.bind(c3), - - b1.save.bind(b1), - b2.save.bind(b2), - b3.save.bind(b3), - - ce1.save.bind(ce1), - ue1.save.bind(ue1), - be1.save.bind(be1), - - ce2.save.bind(ce2), - ue2.save.bind(ue2), - be2.save.bind(be2), - - ce3.save.bind(ce3), - ue3.save.bind(ue3), - be3.save.bind(be3), - - function(next) { - Event. - find({}). - populate('user comment blogpost'). - exec(function(err, docs) { - docs.forEach(function(doc) { - if (doc.__t === 'User4073') { - assert.ok(doc.user.name.indexOf('user') !== -1); - } else if (doc.__t === 'Comment4073') { - assert.ok(doc.comment.content.indexOf('comment') !== -1); - } else if (doc.__t === 'BlogPost4073') { - assert.ok(doc.blogpost.title.indexOf('blog post') !== -1); - } else { - assert.ok(false); - } - }); - next(); - }); - } - ], - done - ); + const docs = [u1, u2, u3, c1, c2, c3, b1, b2, b3, ce1, ue1, be1, ce2, ue2, be2, ce3, ue3, be3]; + + return Promise.all(docs.map(d => d.save())). + then(() => Event.find({}).populate('user comment blogpost')). + then(docs => { + docs.forEach(function(doc) { + if (doc.__t === 'User4073') { + assert.ok(doc.user.name.indexOf('user') !== -1); + } else if (doc.__t === 'Comment4073') { + assert.ok(doc.comment.content.indexOf('comment') !== -1); + } else if (doc.__t === 'BlogPost4073') { + assert.ok(doc.blogpost.title.indexOf('blog post') !== -1); + } else { + assert.ok(false); + } + }); + }); }); it('dynref bug (gh-4104)', function(done) { diff --git a/test/query.test.js b/test/query.test.js index aef6940df9c..ce8a2ae4cf4 100644 --- a/test/query.test.js +++ b/test/query.test.js @@ -2408,6 +2408,7 @@ describe('Query', function() { }); it('throw on sync exceptions in callbacks (gh-6178)', function(done) { + const async = require('async'); const schema = new Schema({}); const Test = db.model('gh6178', schema);