mockingoose
Advanced tools
Comparing version 2.14.0 to 2.15.0
116
lib/index.js
@@ -7,6 +7,2 @@ "use strict"; | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
@@ -18,2 +14,8 @@ | ||
function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } | ||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } | ||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } | ||
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } | ||
@@ -47,3 +49,3 @@ | ||
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(cb) { | ||
var op, modelName, _this$_mongooseOption, _mongooseOptions, Model, mock, err; | ||
var op, modelName, _this$_mongooseOption, _mongooseOptions, Model, mock, err, _iterator, _step, doc, e; | ||
@@ -79,16 +81,69 @@ return regeneratorRuntime.wrap(function _callee$(_context) { | ||
if (mock && !(mock instanceof Model) && !['update', 'updateOne', 'updateMany', 'count', 'countDocuments', 'estimatedDocumentCount', 'distinct'].includes(op)) { | ||
mock = Array.isArray(mock) ? mock.map(function (item) { | ||
return new Model(item); | ||
}) : new Model(mock); | ||
if (!(mock && !(mock instanceof Model) && !['remove', 'deleteOne', 'deleteMany', 'update', 'updateOne', 'updateMany', 'count', 'countDocuments', 'estimatedDocumentCount', 'distinct'].includes(op))) { | ||
_context.next = 33; | ||
break; | ||
} | ||
if (_mongooseOptions.lean) { | ||
mock = Array.isArray(mock) ? mock.map(function (item) { | ||
return item.toObject(); | ||
}) : mock.toObject(); | ||
} | ||
mock = Array.isArray(mock) ? mock.map(function (item) { | ||
return new Model(item); | ||
}) : new Model(mock); | ||
if (!(op === 'insertMany')) { | ||
_context.next = 32; | ||
break; | ||
} | ||
if (!Array.isArray(mock)) mock = [mock]; | ||
_iterator = _createForOfIteratorHelper(mock); | ||
_context.prev = 15; | ||
_iterator.s(); | ||
case 17: | ||
if ((_step = _iterator.n()).done) { | ||
_context.next = 24; | ||
break; | ||
} | ||
doc = _step.value; | ||
e = doc.validateSync(); | ||
if (!e) { | ||
_context.next = 22; | ||
break; | ||
} | ||
throw e; | ||
case 22: | ||
_context.next = 17; | ||
break; | ||
case 24: | ||
_context.next = 29; | ||
break; | ||
case 26: | ||
_context.prev = 26; | ||
_context.t0 = _context["catch"](15); | ||
_iterator.e(_context.t0); | ||
case 29: | ||
_context.prev = 29; | ||
_iterator.f(); | ||
return _context.finish(29); | ||
case 32: | ||
if (_mongooseOptions.lean || _mongooseOptions.rawResult) { | ||
mock = Array.isArray(mock) ? mock.map(function (item) { | ||
return item.toObject(); | ||
}) : mock.toObject(); | ||
} | ||
case 33: | ||
if (!cb) { | ||
_context.next = 13; | ||
_context.next = 35; | ||
break; | ||
@@ -99,5 +154,5 @@ } | ||
case 13: | ||
case 35: | ||
if (!err) { | ||
_context.next = 15; | ||
_context.next = 37; | ||
break; | ||
@@ -108,6 +163,6 @@ } | ||
case 15: | ||
case 37: | ||
return _context.abrupt("return", mock); | ||
case 16: | ||
case 38: | ||
case "end": | ||
@@ -117,3 +172,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this); | ||
}, _callee, this, [[15, 26, 29, 32]]); | ||
})); | ||
@@ -128,3 +183,3 @@ | ||
mongoose.Query.prototype[op] = jest.fn().mockImplementation(function (criteria, doc, options, callback) { | ||
if (['find', 'findOne', 'count', 'countDocuments', 'remove', 'deleteOne', 'deleteMany', 'findOneAndUpdate', 'findOneAndRemove', 'findOneAndDelete', 'findOneAndReplace'].includes(op) && typeof criteria !== 'function') { | ||
if (['find', 'findOne', 'count', 'countDocuments', 'remove', 'deleteOne', 'deleteMany', 'update', 'updateOne', 'updateMany', 'findOneAndUpdate', 'findOneAndRemove', 'findOneAndDelete', 'findOneAndReplace'].includes(op) && typeof criteria !== 'function') { | ||
// find and findOne can take conditions as the first paramter | ||
@@ -240,2 +295,21 @@ // ensure they make it into the Query conditions | ||
}()); | ||
mongoose.Model.insertMany = jest.fn().mockImplementation(function (arr, options, cb) { | ||
var op = 'insertMany'; | ||
var modelName = this.modelName; | ||
if (typeof options === 'function') { | ||
cb = options; | ||
options = null; | ||
} else { | ||
this._mongooseOptions = options; | ||
} | ||
Object.assign(this, { | ||
op: op, | ||
model: { | ||
modelName: modelName | ||
} | ||
}); | ||
return mockedReturn.call(this, cb); | ||
}); | ||
var instance = ['remove', 'save']; | ||
@@ -242,0 +316,0 @@ instance.forEach(function (methodName) { |
{ | ||
"name": "mockingoose", | ||
"version": "2.14.0", | ||
"version": "2.15.0", | ||
"description": "A Jest package for mocking mongoose models", | ||
@@ -5,0 +5,0 @@ "main": "./lib", |
@@ -164,13 +164,14 @@ # Mockingoose [](https://circleci.com/gh/alonronin/mockingoose/tree/master) | ||
- [x] `findOneAndRemove` - for findOneAndRemove query | ||
- [x] `update` - for update query | ||
- [x] `update` - for update query (DEPRECATED) | ||
- [x] `updateOne` - for updateOne query | ||
- [x] `updateMany` - for updateMany query | ||
- [x] `save` - for create, and save documents `Model.create()` or `Model.save()` or `doc.save()` | ||
- [x] `remove` - for remove query | ||
- [x] `remove` - for remove query (DEPRECATED) | ||
- [x] `deleteOne` - for deleteOne query | ||
- [x] `deleteMany` - for deleteMany query | ||
- [x] `aggregate` - for aggregate framework | ||
- [x] `insertMany` - for `Model.insertMany()` bulk insert, can also pass `{ lean: true, rawResult: true }` options. | ||
### Notes | ||
The library is built with Typescript and typings are included. | ||
All operations work with `exec`, `promise` and `callback`. | ||
@@ -184,3 +185,3 @@ | ||
- `update` operation returns original mocked object. | ||
- `deleteOne` and `updateOne` operation returns original mocked object. | ||
@@ -221,2 +222,4 @@ - you can simulate Error by passing an Error to mockingoose: | ||
- you can mock the `Model.exists()` by passing the `findOne` operator. see [Issue #69](https://github.com/alonronin/mockingoose/issues/69) | ||
- no connection is made to the database (mongoose.connect is jest.fn()) | ||
@@ -223,0 +226,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
41391
352
249
0