Skip to content

Instantly share code, notes, and snippets.

@jaredatron
Created July 10, 2017 21:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaredatron/f5b51ce0da1b868cac543acca49c0348 to your computer and use it in GitHub Desktop.
Save jaredatron/f5b51ce0da1b868cac543acca49c0348 to your computer and use it in GitHub Desktop.
haveDuplicates for chai
/*
expect([1,2,3]).to.not.haveDuplicates()
expect([1,1,2,3]).to.haveDuplicates()
*/
chai.Assertion.addMethod('haveDuplicates', function() {
const array = Array.from(this._obj)
expect(array).to.be.an.instanceof(Array)
const dups = []
array.forEach((member, index) => {
if (array.lastIndexOf(member) !== index){
dups.push(member)
}
})
this.assert(
dups.length !== 0,
"expected #{this} to have duplicates",
"expected #{this} to not have duplicates",
[], // expected
dups // actual
);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment