Last active
August 1, 2016 21:16
-
-
Save hughdunne/ee8fd7e313df0ef008be to your computer and use it in GitHub Desktop.
Some useful extensions for expect.js
This file contains 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
// 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