Created
July 10, 2017 21:28
-
-
Save jaredatron/f5b51ce0da1b868cac543acca49c0348 to your computer and use it in GitHub Desktop.
haveDuplicates for chai
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
/* | |
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