forked from faker-js/faker
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: extend vitest with toBeUnique (faker-js#1428)
- Loading branch information
1 parent
46c1612
commit a589e10
Showing
3 changed files
with
42 additions
and
17 deletions.
There are no files selected for viewing
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 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { expect } from 'vitest'; | ||
|
||
expect.extend({ | ||
toContainDuplicates<T>(received: T[]) { | ||
const { isNot } = this; | ||
|
||
const uniques = new Set(received); | ||
const duplications = received.filter((entry) => !uniques.delete(entry)); | ||
const uniqueDuplication = [...new Set(duplications)]; | ||
|
||
return { | ||
pass: uniqueDuplication.length !== 0, | ||
message: () => | ||
isNot | ||
? `Duplicated values are [${uniqueDuplication.join(', ')}]` | ||
: `No duplicate values in [${received.join(', ')}]`, | ||
}; | ||
}, | ||
}); | ||
|
||
interface CustomMatchers { | ||
toContainDuplicates(): void; | ||
} | ||
|
||
declare global { | ||
// eslint-disable-next-line @typescript-eslint/no-namespace | ||
namespace Vi { | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface Assertion extends CustomMatchers {} | ||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface AsymmetricMatchersContaining extends CustomMatchers {} | ||
} | ||
} |