Skip to content

Instantly share code, notes, and snippets.

@hughdunne
Last active August 1, 2016 21:16
Show Gist options
  • Save hughdunne/ee8fd7e313df0ef008be to your computer and use it in GitHub Desktop.
Save hughdunne/ee8fd7e313df0ef008be to your computer and use it in GitHub Desktop.
Some useful extensions for expect.js
// This allows us to assert that an object has the specified key.
// Alternatively we could say expect(obj.key).toExist()
// but the problem is that if the test fails, we only see: "Expected undefined to exist"
// and we don't know which property is missing.
expect.extend({
toHaveProperty(prop){
expect.assert(prop in this.actual, "Expected object to have property %s", prop)
},
});
// Building on previous extension, this lets us assert
// that an object has all keys in the specified list.
expect.extend({
toHaveProperties(propList){
propList.forEach(function(prop) {
expect(this.actual).toHaveProperty(prop)
}, this)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment