Created
July 5, 2017 07:53
-
-
Save joelmukuthu/02092afb36c89808b1f449d8ac792866 to your computer and use it in GitHub Desktop.
unexpected-mongoose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Model, Types: { ObjectId, Document } } = require('mongoose'); | |
module.exports = { | |
name: 'unexpected-mongan', | |
installInto: expect => { | |
expect.use(require('unexpected-set')); | |
expect.addType({ | |
name: 'MongooseModel', | |
identify(val) { | |
return val instanceof Model; | |
}, | |
equal(a, b, equal) { | |
return ( | |
a.constructor.modelName === b.constructor.modelName && | |
equal(a.toObject(), b.toObject()) | |
); | |
} | |
}); | |
expect.addType({ | |
name: 'MongooseDocument', | |
identify(val) { | |
return val instanceof Document; | |
}, | |
equal(a, b, equal) { | |
return equal(a.toObject(), b.toObject()); | |
} | |
}); | |
expect.addType({ | |
name: 'MongooseModelArray', | |
base: 'array', | |
identify(val) { | |
return ( | |
val && | |
Array.isArray(val) && | |
val.length && | |
val.every(model => model instanceof Model) | |
); | |
} | |
}); | |
expect.addType({ | |
name: 'MongooseDocumentArray', | |
base: 'array', | |
identify(val) { | |
return ( | |
val && | |
Array.isArray(val) && | |
val.length && | |
val.every(doc => doc instanceof Document) | |
); | |
} | |
}); | |
expect.addAssertion( | |
'<MongooseModel> to satisfy <object>', | |
(expect, subject, value) => { | |
return expect(subject.toObject(), 'to satisfy', value); | |
} | |
); | |
expect.addAssertion( | |
'<MongooseModel> to equal <object>', | |
(expect, subject, value) => { | |
return expect(subject.toObject(), 'to equal', value); | |
} | |
); | |
expect.addAssertion( | |
'<MongooseDocument> to satisfy <object>', | |
(expect, subject, value) => { | |
return expect(subject.toObject(), 'to satisfy', value); | |
} | |
); | |
expect.addAssertion( | |
'<MongooseDocument> to equal <object>', | |
(expect, subject, value) => { | |
return expect(subject.toObject(), 'to equal', value); | |
} | |
); | |
expect.addAssertion( | |
'<MongooseModelArray|MongooseDocumentArray> to [exhaustively] satisfy <array>', | |
(expect, subject, value) => { | |
return expect( | |
subject, | |
'with set semantics', | |
'to [exhaustively] satisfy', | |
value | |
); | |
} | |
); | |
expect.addAssertion( | |
'<MongooseModelArray|MongooseDocumentArray> to equal <array>', | |
(expect, subject, value) => { | |
return expect(subject, 'with set semantics', 'to equal', value); | |
} | |
); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
take these models:
then in a test, add countries to the continent and assert that those countries were added:
What i'm trying to get to work is the
<MongooseDocumentArray> to satisfy <array>
assertion on thecountries
field. I can't guarantee the order of items in the array so I just want to usewith set semantics
to ensure that those items are there, regardless of the order in which they are.The problem is that unexpected does not identify the array of countries as a
MongooseDocumentArray
because in asserting thateurope
satisfies the RHS object, it first uses the<MongooseModel> to satisfy <object>
assertion, thereby callingeurope.toObject()
which means that by the time it's checking thateurope.countries
satisfies the RHS array,europe.countries
is not aMongooseDocumentArray
anymore, but just an array of plain objects. FYI, callingtoObject
on a mongoose model has the effect of callingtoObject
on any nested documents, in this case the documents incountries
.Is there a better way to do this, besides
expect(europe.countries, 'to satisfy', [])
?PS: I'm aware that the array types might be bad for performance but I'm ignoring that for now :)