Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.