Skip to content

Commit

Permalink
test: extend vitest with toBeUnique (faker-js#1428)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 authored and wael-fadlallah committed Oct 14, 2022
1 parent 46c1612 commit a589e10
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
14 changes: 7 additions & 7 deletions test/helpers.spec.ts
Expand Up @@ -2,6 +2,7 @@ import { afterEach, describe, expect, it } from 'vitest';
import { faker, FakerError } from '../src';
import { luhnCheck } from '../src/modules/helpers/luhn-check';
import { seededTests } from './support/seededRuns';
import './vitest-extensions';

const NON_SEEDED_BASED_RUN = 5;

Expand Down Expand Up @@ -142,7 +143,7 @@ describe('helpers', () => {
});

// Check uniqueness
expect(subset).toHaveLength(new Set(subset).size);
expect(subset).not.toContainDuplicates();
});

it('should return a subset of fixed length with random elements in the array', () => {
Expand Down Expand Up @@ -345,8 +346,8 @@ describe('helpers', () => {
const input = ['a', 'a', 'a', 'a,', 'a', 'a', 'a', 'a', 'b'];
const length = 2;
const unique = faker.helpers.uniqueArray(input, length);
expect(unique).not.toContainDuplicates();
expect(unique).toHaveLength(length);
expect(new Set(unique).size).toBe(length);
});

it('definition array returns unique array', () => {
Expand All @@ -355,31 +356,30 @@ describe('helpers', () => {
faker.definitions.hacker.noun,
length
);
expect(unique).not.toContainDuplicates();
expect(unique).toHaveLength(length);
expect(new Set(unique).size).toBe(length);
});

it('function returns unique array', () => {
const length = faker.datatype.number({ min: 1, max: 6 });
const unique = faker.helpers.uniqueArray(faker.lorem.word, length);
expect(unique).not.toContainDuplicates();
expect(unique).toHaveLength(length);
expect(new Set(unique).size).toBe(length);
});

it('empty array returns empty array', () => {
const input = [];
const length = faker.datatype.number({ min: 1, max: 6 });
const unique = faker.helpers.uniqueArray(input, length);
expect(unique).toHaveLength(input.length);
expect(new Set(unique).size).toBe(input.length);
expect(unique).toHaveLength(0);
});

it('length longer than source returns max length', () => {
const input = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];
const length = input.length + 1;
const unique = faker.helpers.uniqueArray(input, length);
expect(unique).not.toContainDuplicates();
expect(unique).toHaveLength(input.length);
expect(new Set(unique).size).toBe(input.length);
});

it('works as expected when seeded', () => {
Expand Down
12 changes: 2 additions & 10 deletions test/locales.spec.ts
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';
import type { LocaleDefinition } from '../src';
import { faker } from '../src';
import allLocales from '../src/locales';
import './vitest-extensions';

// Remark: actual use of locales functionality is currently tested in all.functional.js test

Expand Down Expand Up @@ -32,16 +33,7 @@ describe('locale', () => {
describe(definitionName, () => {
function testArraySample<T>(arr: T[]) {
it('should not have duplicate entries', () => {
const uniques = new Set(arr);
const duplications = arr.filter(
(entry) => !uniques.delete(entry)
);
const uniqueDuplication = [...new Set(duplications)];

expect(
uniqueDuplication,
`Duplicated values are: [${uniqueDuplication.join(', ')}]`
).toEqual([]);
expect(arr).not.toContainDuplicates();
});
}

Expand Down
33 changes: 33 additions & 0 deletions test/vitest-extensions.ts
@@ -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 {}
}
}

0 comments on commit a589e10

Please sign in to comment.