Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: extend vitest with (not) toContainDuplicates #1428

Merged
merged 4 commits into from Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {}
}
}