From feb963d6c77a8f69f43ce1cfbe2ebdfbb06ef5bc Mon Sep 17 00:00:00 2001 From: alonronin Date: Mon, 24 Dec 2018 15:06:23 +0200 Subject: [PATCH] :wrench: build --- lib/index.js | 37 ++++++++++++++++++++++++++++++++++++- lib/index.js.map | 2 +- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 2d3f6df..552fdbc 100644 --- a/lib/index.js +++ b/lib/index.js @@ -126,6 +126,8 @@ var instance = ['remove', 'save']; instance.forEach(function (methodName) { _mongoose2.default.Model.prototype[methodName] = jest.fn().mockImplementation(function (options, cb) { + var _this = this; + var op = methodName; var modelName = this.constructor.modelName; @@ -134,7 +136,40 @@ instance.forEach(function (methodName) { Object.assign(this, { op: op, model: { modelName: modelName } }); - return mockedReturn.call(this, cb); + var hooks = this.constructor.hooks; + + return new Promise(function (resolve, reject) { + hooks.execPre(op, _this, [cb], function (err) { + if (err) { + reject(err); + return; + } + + var ret = mockedReturn.call(_this, cb); + + if (cb) { + hooks.execPost(op, _this, [ret], function (err) { + if (err) { + reject(err); + return; + } + + resolve(ret); + }); + } else { + ret.then(function (ret) { + hooks.execPost(op, _this, [ret], function (err) { + if (err) { + reject(err); + return; + } + + resolve(ret); + }); + }).catch(reject); + } + }); + }); }); }); diff --git a/lib/index.js.map b/lib/index.js.map index c729f83..0478bc7 100644 --- a/lib/index.js.map +++ b/lib/index.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/index.js"],"names":["test","mongoose","version","Promise","connect","jest","fn","mockImplementation","resolve","createConnection","mockReturnValue","on","once","then","catch","model","bind","ops","mockedReturn","cb","op","modelName","_mongooseOptions","Model","mock","mockingoose","__mocks","err","Error","includes","Array","isArray","map","item","lean","toObject","reject","forEach","Query","prototype","criteria","doc","options","callback","arguments","length","undefined","exec","call","Aggregate","_model","aggregate","instance","methodName","constructor","Object","assign","doMock","target","resetAll","toJSON","traps","get","prop","hasOwnProperty","Reflect","toReturn","o","reset","Proxy"],"mappings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AqB3C,EAArB,IAA2B+C,CAD/B,GAEIT,OAAOhC,OAAP,CAAeqC,IAAf,wBAA0B3C,EAA1B,EAA+B+C,CAA/B,CAFJ;;AAIA,eAAO,IAAP;AACD,OAPI;AASLC,WATK,iBASChD,EATD,EASK;AACRA,cAAM,OAAOsC,OAAOhC,OAAP,CAAeqC,IAAf,EAAqB3C,EAArB,CAAb,IAAyC,OAAOsC,OAAOhC,OAAP,CAAeqC,IAAf,CAAhD;;AAEA,eAAO,IAAP;AACD,OAbI;AAeLH,YAfK,oBAeI;AACP,eAAOF,OAAOhC,OAAP,CAAeqC,IAAf,KAAwB,EAA/B;AACD;AAjBI,KAAP;AAmBD;AAvBW,CAAd;;AA0BA,IAAMtC,cAAc,IAAI4C,KAAJ,CAAUX,MAAV,EAAkBG,KAAlB,CAApB;;kBAEepC,W","file":"index.js","sourcesContent":["import mongoose from 'mongoose';\r\n\r\nif(!/^5/.test(mongoose.version)) mongoose.Promise = Promise;\r\n\r\nmongoose.connect = jest.fn().mockImplementation(() => Promise.resolve());\r\nmongoose.createConnection = jest\r\n .fn()\r\n .mockReturnValue({\r\n on: jest.fn(),\r\n once: jest.fn(),\r\n then(resolve) { return Promise.resolve(resolve(this)); },\r\n catch() {},\r\n model: mongoose.model.bind(mongoose),\r\n });\r\n\r\nconst ops = [\r\n 'find',\r\n 'findOne',\r\n 'count',\r\n 'countDocuments',\r\n 'estimatedDocumentCount',\r\n 'distinct',\r\n 'findOneAndUpdate',\r\n 'findOneAndRemove',\r\n 'remove',\r\n 'update',\r\n 'deleteOne',\r\n 'deleteMany',\r\n];\r\n\r\nconst mockedReturn = function (cb) {\r\n const { op, model: { modelName }, _mongooseOptions = {} } = this;\r\n const Model = mongoose.model(modelName);\r\n\r\n let mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName][op];\r\n\r\n let err = null;\r\n\r\n if (mock instanceof Error) err = mock;\r\n\r\n if (!mock && op === 'save') { mock = this;}\r\n\r\n if (mock && mock instanceof Model === false && (!['update', 'count', 'countDocuments', 'estimatedDocumentCount'].includes(op))) {\r\n mock = Array.isArray(mock) ? mock.map(item => new Model(item)) : new Model(mock);\r\n\r\n if (_mongooseOptions.lean) mock = Array.isArray(mock) ? mock.map(item => item.toObject()) : mock.toObject();\r\n }\r\n\r\n if (cb) return cb(err, mock);\r\n\r\n if (err) return Promise.reject(err);\r\n\r\n return Promise.resolve(mock);\r\n};\r\n\r\nops.forEach(op => {\r\n mongoose.Query.prototype[op] = jest.fn().mockImplementation(function (criteria, doc, options, callback) {\r\n switch (arguments.length) {\r\n case 4:\r\n case 3:\r\n if (typeof options === 'function') {\r\n callback = options;\r\n options = {};\r\n }\r\n break;\r\n case 2:\r\n if (typeof doc === 'function') {\r\n callback = doc;\r\n doc = criteria;\r\n criteria = undefined;\r\n }\r\n options = undefined;\r\n break;\r\n case 1:\r\n if (typeof criteria === 'function') {\r\n callback = criteria;\r\n criteria = options = doc = undefined;\r\n } else {\r\n doc = criteria;\r\n criteria = options = undefined;\r\n }\r\n }\r\n\r\n this.op = op;\r\n\r\n if (!callback) return this;\r\n\r\n return this.exec.call(this, callback);\r\n });\r\n});\r\n\r\nmongoose.Query.prototype.exec = jest.fn().mockImplementation(function cb(cb) {\r\n return mockedReturn.call(this, cb);\r\n});\r\n\r\nmongoose.Aggregate.prototype.exec = jest.fn().mockImplementation(function cb(cb) {\r\n\tconst { _model: { modelName } } = this;\r\n\r\n\tlet mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName].aggregate;\r\n\r\n\tlet err = null;\r\n\r\n\tif (mock instanceof Error) err = mock;\r\n\r\n\tif (cb) return cb(err, mock);\r\n\r\n\tif (err) return Promise.reject(err);\r\n\r\n\treturn Promise.resolve(mock);\r\n});\r\n\r\nconst instance = [\r\n 'remove',\r\n 'save'\r\n];\r\n\r\ninstance.forEach(methodName => {\r\n mongoose.Model.prototype[methodName] = jest.fn().mockImplementation(function (options, cb) {\r\n const op = methodName;\r\n const { modelName } = this.constructor;\r\n\r\n if (typeof options === 'function') cb = options;\r\n\r\n Object.assign(this, { op, model: { modelName } });\r\n\r\n return mockedReturn.call(this, cb);\r\n })\r\n});\r\n\r\njest.doMock('mongoose', () => mongoose);\r\n\r\nconst target = {\r\n __mocks: {},\r\n resetAll() { this.__mocks = {}; },\r\n toJSON() { return this.__mocks; },\r\n};\r\n\r\nconst traps = {\r\n get(target, prop) {\r\n if (target.hasOwnProperty(prop)) return Reflect.get(target, prop);\r\n\r\n return {\r\n toReturn(o, op = 'find') {\r\n target.__mocks.hasOwnProperty(prop)\r\n ? target.__mocks[prop][op] = o\r\n : target.__mocks[prop] = { [op]: o };\r\n\r\n return this;\r\n },\r\n\r\n reset(op) {\r\n op && delete target.__mocks[prop][op] || delete target.__mocks[prop];\r\n\r\n return this;\r\n },\r\n\r\n toJSON() {\r\n return target.__mocks[prop] || {};\r\n },\r\n };\r\n },\r\n};\r\n\r\nconst mockingoose = new Proxy(target, traps);\r\n\r\nexport default mockingoose;\r\n"]} \ No newline at end of file +{"version":3,"sources":["../src/index.js"],"names":["test","mongoose","version","Promise","connect","jest","fn","mockImplementation","resolve","createConnection","mockReturnValue","on","once","then","catch","model","bind","ops","mockedReturn","cb","op","modelName","_mongooseOptions","Model","mock","mockingoose","__mocks","err","Error","includes","Array","isArray","map","item","lean","toObject","reject","forEach","Query","prototype","criteria","doc","options","callback","arguments","length","undefined","exec","call","Aggregate","_model","aggregate","instance","methodName","constructor","Object","assign","hooks","execPre","ret","execPost","doMock","target","resetAll","toJSON","traps","get","prop","hasOwnProperty","Reflect","toReturn","o","reset","Proxy"],"mappings":";;;;;;AAAA;;;;;;;;AAEA,IAAG,CAAC,KAAKA,IAAL,CAAUC,mBAASC,OAAnB,CAAJ,EAAiCD,mBAASE,OAAT,GAAmBA,OAAnB;;AAEjCF,mBAASG,OAAT,GAAmBC,KAAKC,EAAL,GAAUC,kBAAV,CAA6B;AAAA,SAAMJ,QAAQK,OAAR,EAAN;AAAA,CAA7B,CAAnB;AACAP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est.fn().mockImplementation(() => Promise.resolve());\nmongoose.createConnection = jest\n .fn()\n .mockReturnValue({\n on: jest.fn(),\n once: jest.fn(),\n then(resolve) { return Promise.resolve(resolve(this)); },\n catch() {},\n model: mongoose.model.bind(mongoose),\n });\n\nconst ops = [\n 'find',\n 'findOne',\n 'count',\n 'countDocuments',\n 'estimatedDocumentCount',\n 'distinct',\n 'findOneAndUpdate',\n 'findOneAndRemove',\n 'remove',\n 'update',\n 'deleteOne',\n 'deleteMany',\n];\n\nconst mockedReturn = function (cb) {\n const { op, model: { modelName }, _mongooseOptions = {} } = this;\n const Model = mongoose.model(modelName);\n\n let mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName][op];\n\n let err = null;\n\n if (mock instanceof Error) err = mock;\n\n if (!mock && op === 'save') { mock = this;}\n\n if (mock && mock instanceof Model === false && (!['update', 'count', 'countDocuments', 'estimatedDocumentCount'].includes(op))) {\n mock = Array.isArray(mock) ? mock.map(item => new Model(item)) : new Model(mock);\n\n if (_mongooseOptions.lean) mock = Array.isArray(mock) ? mock.map(item => item.toObject()) : mock.toObject();\n }\n\n if (cb) return cb(err, mock);\n\n if (err) return Promise.reject(err);\n\n return Promise.resolve(mock);\n};\n\nops.forEach(op => {\n mongoose.Query.prototype[op] = jest.fn().mockImplementation(function (criteria, doc, options, callback) {\n switch (arguments.length) {\n case 4:\n case 3:\n if (typeof options === 'function') {\n callback = options;\n options = {};\n }\n break;\n case 2:\n if (typeof doc === 'function') {\n callback = doc;\n doc = criteria;\n criteria = undefined;\n }\n options = undefined;\n break;\n case 1:\n if (typeof criteria === 'function') {\n callback = criteria;\n criteria = options = doc = undefined;\n } else {\n doc = criteria;\n criteria = options = undefined;\n }\n }\n\n this.op = op;\n\n if (!callback) return this;\n\n return this.exec.call(this, callback);\n });\n});\n\nmongoose.Query.prototype.exec = jest.fn().mockImplementation(function cb(cb) {\n return mockedReturn.call(this, cb);\n});\n\nmongoose.Aggregate.prototype.exec = jest.fn().mockImplementation(function cb(cb) {\n\tconst { _model: { modelName } } = this;\n\n\tlet mock = mockingoose.__mocks[modelName] && mockingoose.__mocks[modelName].aggregate;\n\n\tlet err = null;\n\n\tif (mock instanceof Error) err = mock;\n\n\tif (cb) return cb(err, mock);\n\n\tif (err) return Promise.reject(err);\n\n\treturn Promise.resolve(mock);\n});\n\nconst instance = [\n 'remove',\n 'save'\n];\n\ninstance.forEach(methodName => {\n mongoose.Model.prototype[methodName] = jest.fn().mockImplementation(function (options, cb) {\n const op = methodName;\n const { modelName } = this.constructor;\n\n if (typeof options === 'function') cb = options;\n\n Object.assign(this, { op, model: { modelName } });\n\n const hooks = this.constructor.hooks\n\n return new Promise((resolve, reject) => {\n hooks.execPre(op, this, [cb], (err) => {\n if (err) {\n reject(err);\n return;\n }\n\n const ret = mockedReturn.call(this, cb);\n\n if (cb) {\n hooks.execPost(op, this, [ret], (err) => {\n if (err) {\n reject(err);\n return;\n }\n\n resolve(ret);\n });\n } else {\n ret\n .then((ret) => {\n hooks.execPost(op, this, [ret], (err) => {\n if (err) {\n reject(err);\n return;\n }\n\n resolve(ret);\n });\n })\n .catch(reject);\n }\n });\n });\n });\n});\n\njest.doMock('mongoose', () => mongoose);\n\nconst target = {\n __mocks: {},\n resetAll() { this.__mocks = {}; },\n toJSON() { return this.__mocks; },\n};\n\nconst traps = {\n get(target, prop) {\n if (target.hasOwnProperty(prop)) return Reflect.get(target, prop);\n\n return {\n toReturn(o, op = 'find') {\n target.__mocks.hasOwnProperty(prop)\n ? target.__mocks[prop][op] = o\n : target.__mocks[prop] = { [op]: o };\n\n return this;\n },\n\n reset(op) {\n op && delete target.__mocks[prop][op] || delete target.__mocks[prop];\n\n return this;\n },\n\n toJSON() {\n return target.__mocks[prop] || {};\n },\n };\n },\n};\n\nconst mockingoose = new Proxy(target, traps);\n\nexport default mockingoose;\n"]} \ No newline at end of file