Note: I have also opened an issue for this on the Waterline repo but not gotten a simpler solution than my workaround.
I have a User model with all the default attributes (e.g. createdDate
, modifiedDate
) and a few custom ones like emailAddress
, password
(suppressed in toJSON()
), etc. and some additional associations with other models.
What is the correct way to compare a controller response consisting of a User model instance in JSON format to a Waterline model instance?
For example, this workaround works:
request
.get('/user/1')
.end(function (err, res) {
expect(res.body).to.deep.equal(
JSON.parse(JSON.stringify( // Workaround for toJSON not stringifying dates
_.cloneDeep( // Workaround for associations being lost
testUser.toJSON() // `testUser` is a Waterline model instance that should be equivalent to the requested data (`/user/1`)
)
))
);
done();
});
But just about everything else I've tried doesn't; either associations are lost (the cloneDeep()
resolves this) or stringified dates are being compared to Date objects (the JSON.parse(JSON.stringify())
resolves this).
Is there a better way to compare JSON with a Waterline model instance? Perhaps some built-in utility?
What about using JSON schema?