aboutsummaryrefslogtreecommitdiff
path: root/test/vitest-extensions.ts
blob: b33919efb007ce8447b5c7d5792cf4d86182a43e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 {}
  }
}