mockingoose
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -5,56 +5,135 @@ import mockingoose from '../src/index'; | ||
describe('mockingoose', () => { | ||
it('should validate', () => { | ||
const user = new User({ | ||
name: 'user', | ||
email: 'user@email.com' | ||
describe('explicit tests', () => { | ||
it('should validate', () => { | ||
const user = new User({ | ||
name: 'user', | ||
email: 'user@email.com' | ||
}); | ||
return user.validate().then(() => { | ||
expect(user.toObject()).toHaveProperty('created'); | ||
expect(user.toObject()).toHaveProperty('_id') | ||
}) | ||
}); | ||
return user.validate().then(() => { | ||
expect(user.toObject()).toHaveProperty('created'); | ||
expect(user.toObject()).toHaveProperty('_id') | ||
}) | ||
}); | ||
it('should find', () => { | ||
mockingoose.User.toReturn({ name: 2 }); | ||
it('should find', () => { | ||
mockingoose.User.toReturn({ name: 2 }); | ||
return User | ||
.find() | ||
.where('name') | ||
.in([1]) | ||
.then(result => { | ||
expect(result).toEqual({ name: 2 }); | ||
}) | ||
}); | ||
return User | ||
.find() | ||
.where('name') | ||
.in([1]) | ||
.then(result => { | ||
expect(result).toEqual({ name: 2 }); | ||
}) | ||
}); | ||
it('should update with exec and callback', (done) => { | ||
mockingoose.User.toReturn({ email: 'alon@ronin.co.il' }, 'update'); | ||
it('should update', () => { | ||
mockingoose.User.toReturn({ email: 'alon@ronin.co.il' }, 'update'); | ||
User | ||
.update({ email: 'alon@ronin.co.il' }) | ||
.where('name', 'alon') | ||
.exec((err, result) => { | ||
expect(result).toEqual({ email: 'alon@ronin.co.il' }); | ||
done(); | ||
}) | ||
}); | ||
return User | ||
.update({ email: 'alon@ronin.co.il' }) | ||
.where('name', 'alon') | ||
.exec((err, result) => { | ||
expect(result).toEqual({ email: 'alon@ronin.co.il' }); | ||
it('should create', () => { | ||
mockingoose.User.toReturn({ _id: '1' }, 'save'); | ||
return User | ||
.create({ email: 'alon@ronin.co.il' }) | ||
.then(result => { | ||
expect(result).toEqual({ _id: '1' }); | ||
}) | ||
}); | ||
it('should return error', () => { | ||
mockingoose.User.toReturn(new Error(), 'save'); | ||
return User | ||
.create({ email: 'alon@ronin.co.il' }) | ||
.catch(err => { | ||
expect(err).toBeInstanceOf(Error); | ||
}) | ||
}); | ||
it('should find with callback', (done) => { | ||
const _doc = { name: 'alon' }; | ||
mockingoose.User.toReturn(_doc); | ||
User.find({ _id: 1}, (err, doc) => { | ||
expect(err).toBeNull(); | ||
expect(doc).toBe(_doc); | ||
done(); | ||
}) | ||
}) | ||
}); | ||
it('should create', () => { | ||
mockingoose.User.toReturn({ _id: '1' }, 'save'); | ||
describe('check all operations', () => { | ||
const ops = [ | ||
'find', | ||
'findOne', | ||
'count', | ||
'distinct', | ||
'findOneAndUpdate', | ||
'findOneAndRemove', | ||
'remove', | ||
'deleteOne', | ||
'deleteMany' | ||
]; | ||
return User | ||
.create({ email: 'alon@ronin.co.il' }) | ||
.then(result => { | ||
expect(result).toEqual({ _id: '1' }); | ||
describe('with promise', () => { | ||
ops.forEach(op => { | ||
it(op, () => { | ||
const mocked = { | ||
op | ||
}; | ||
mockingoose.User.toReturn(mocked, op); | ||
return User[op]().then(doc => expect(doc).toBe(mocked)); | ||
}); | ||
}) | ||
}); | ||
describe('with exec and callback', () => { | ||
ops.forEach(op => { | ||
it(op, (done) => { | ||
const mocked = { | ||
op | ||
}; | ||
mockingoose.User.toReturn(mocked, op); | ||
User[op]().exec((err, doc) => { | ||
expect(err).toBeNull(); | ||
expect(doc).toBe(mocked); | ||
done(); | ||
}); | ||
}); | ||
}) | ||
}); | ||
describe('with callback', () => { | ||
ops.forEach(op => { | ||
it(op, (done) => { | ||
const mocked = { | ||
op | ||
}; | ||
mockingoose.User.toReturn(mocked, op); | ||
User[op]((err, doc) => { | ||
expect(err).toBeNull(); | ||
expect(doc).toBe(mocked); | ||
done(); | ||
}); | ||
}); | ||
}) | ||
}) | ||
}); | ||
}); | ||
it('should return error', () => { | ||
mockingoose.User.toReturn(new Error(), 'save'); | ||
return User | ||
.create({ email: 'alon@ronin.co.il' }) | ||
.catch(err => { | ||
expect(err).toBeInstanceOf(Error); | ||
}) | ||
}) | ||
}); |
@@ -18,2 +18,4 @@ 'use strict'; | ||
var ops = ['find', 'findOne', 'count', 'distinct', 'findOneAndUpdate', 'findOneAndRemove', 'remove', 'deleteOne', 'deleteMany']; | ||
var mockedReturn = function mockedReturn(op, modelName, cb) { | ||
@@ -31,2 +33,12 @@ var mock = mockingoose.__mocks[modelName][op]; | ||
ops.forEach(function (op) { | ||
_mongoose2.default.Query.prototype[op] = jest.fn().mockImplementation(function (condition, cb) { | ||
this.op = op; | ||
if (!cb) return this; | ||
return this.exec.call(this, cb); | ||
}); | ||
}); | ||
_mongoose2.default.Query.prototype.exec = jest.fn().mockImplementation(function cb(cb) { | ||
@@ -33,0 +45,0 @@ var op = this.op, |
{ | ||
"name": "mockingoose", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A Jest package for mocking mongoose models", | ||
@@ -33,3 +33,6 @@ "main": "./lib", | ||
"mongoose": "^4.11.1" | ||
}, | ||
"jest": { | ||
"testEnvironment": "node" | ||
} | ||
} |
@@ -69,8 +69,17 @@ # Mockingoose | ||
[x] `find` - for all find queries | ||
[x] `update` - for update queries | ||
[x] `save` - for create, and save queries | ||
[] `remove` - for removing documents | ||
- [x] `find` - for find query | ||
- [x] `findOne` - for findOne query | ||
- [x] `count` - for count query | ||
- [ ] `distinct` - for distinct query | ||
- [ ] `findOneAndUpdate` - for findOneAndUpdate query | ||
- [ ] `findOneAndRemove` - for findOneAndRemove query | ||
- [x] `update` - for update query | ||
- [x] `save` - for create, and save documents `Model.create()` or `Model.save()` or `doc.save()` | ||
- [x] `remove` - for remove query | ||
- [ ] `deleteOne` - for deleteOne query | ||
- [ ] `deleteMany` - for deleteMany query | ||
### Notes | ||
All operations works with `exec` or `promise`. | ||
the unchecked ones does not work with node callback syntax. | ||
@@ -77,0 +86,0 @@ if you are using `Model.create` and you don't pass a mock with mockingoose, |
@@ -6,2 +6,14 @@ import mongoose from 'mongoose'; | ||
const ops = [ | ||
'find', | ||
'findOne', | ||
'count', | ||
'distinct', | ||
'findOneAndUpdate', | ||
'findOneAndRemove', | ||
'remove', | ||
'deleteOne', | ||
'deleteMany' | ||
]; | ||
const mockedReturn = (op, modelName, cb) => { | ||
@@ -19,2 +31,12 @@ const mock = mockingoose.__mocks[modelName][op]; | ||
ops.forEach(op => { | ||
mongoose.Query.prototype[op] = jest.fn().mockImplementation(function (condition, cb) { | ||
this.op = op; | ||
if(!cb) return this; | ||
return this.exec.call(this, cb); | ||
}) | ||
}); | ||
mongoose.Query.prototype.exec = jest.fn().mockImplementation(function cb(cb) { | ||
@@ -21,0 +43,0 @@ const { op, model: { modelName }} = this; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17056
237
105