From 693ce04ff4095157c3c3b38f789cf87f9890274f Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 11 Oct 2022 19:53:28 +0200 Subject: [PATCH 1/2] test: extend vitest --- test/helpers.spec.ts | 14 +++++++------- test/locales.spec.ts | 12 ++---------- test/vitest-extensions.ts | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 17 deletions(-) create mode 100644 test/vitest-extensions.ts diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index ce8fb73387e..98542218ab6 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -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; @@ -142,7 +143,7 @@ describe('helpers', () => { }); // Check uniqueness - expect(subset).toHaveLength(new Set(subset).size); + expect(subset).toBeUnique(); }); it('should return a subset of fixed length with random elements in the array', () => { @@ -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).toBeUnique(); expect(unique).toHaveLength(length); - expect(new Set(unique).size).toBe(length); }); it('definition array returns unique array', () => { @@ -355,31 +356,30 @@ describe('helpers', () => { faker.definitions.hacker.noun, length ); + expect(unique).toBeUnique(); 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).toBeUnique(); 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).toBeUnique(); expect(unique).toHaveLength(input.length); - expect(new Set(unique).size).toBe(input.length); }); it('works as expected when seeded', () => { diff --git a/test/locales.spec.ts b/test/locales.spec.ts index 17115809405..776510c63d7 100644 --- a/test/locales.spec.ts +++ b/test/locales.spec.ts @@ -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 @@ -32,16 +33,7 @@ describe('locale', () => { describe(definitionName, () => { function testArraySample(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).toBeUnique(); }); } diff --git a/test/vitest-extensions.ts b/test/vitest-extensions.ts new file mode 100644 index 00000000000..b324b876d5e --- /dev/null +++ b/test/vitest-extensions.ts @@ -0,0 +1,28 @@ +import { expect } from 'vitest'; + +expect.extend({ + toBeUnique(received: T[]) { + const uniques = new Set(received); + const duplications = received.filter((entry) => !uniques.delete(entry)); + const uniqueDuplication = [...new Set(duplications)]; + + return { + pass: uniqueDuplication.length === 0, + message: () => `Duplicated values are: [${uniqueDuplication.join(', ')}]`, + }; + }, +}); + +interface CustomMatchers { + toBeUnique(): 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 {} + } +} From e2fb8c8e4b7c9f732bd292659afaacc380d5313c Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Wed, 12 Oct 2022 12:30:45 +0200 Subject: [PATCH 2/2] test: extend vitest --- test/helpers.spec.ts | 10 +++++----- test/locales.spec.ts | 2 +- test/vitest-extensions.ts | 13 +++++++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 98542218ab6..15c26874d2f 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -143,7 +143,7 @@ describe('helpers', () => { }); // Check uniqueness - expect(subset).toBeUnique(); + expect(subset).not.toContainDuplicates(); }); it('should return a subset of fixed length with random elements in the array', () => { @@ -346,7 +346,7 @@ 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).toBeUnique(); + expect(unique).not.toContainDuplicates(); expect(unique).toHaveLength(length); }); @@ -356,14 +356,14 @@ describe('helpers', () => { faker.definitions.hacker.noun, length ); - expect(unique).toBeUnique(); + expect(unique).not.toContainDuplicates(); expect(unique).toHaveLength(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).toBeUnique(); + expect(unique).not.toContainDuplicates(); expect(unique).toHaveLength(length); }); @@ -378,7 +378,7 @@ describe('helpers', () => { 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).toBeUnique(); + expect(unique).not.toContainDuplicates(); expect(unique).toHaveLength(input.length); }); diff --git a/test/locales.spec.ts b/test/locales.spec.ts index 776510c63d7..9e290c5a559 100644 --- a/test/locales.spec.ts +++ b/test/locales.spec.ts @@ -33,7 +33,7 @@ describe('locale', () => { describe(definitionName, () => { function testArraySample(arr: T[]) { it('should not have duplicate entries', () => { - expect(arr).toBeUnique(); + expect(arr).not.toContainDuplicates(); }); } diff --git a/test/vitest-extensions.ts b/test/vitest-extensions.ts index b324b876d5e..b33919efb00 100644 --- a/test/vitest-extensions.ts +++ b/test/vitest-extensions.ts @@ -1,20 +1,25 @@ import { expect } from 'vitest'; expect.extend({ - toBeUnique(received: T[]) { + toContainDuplicates(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: () => `Duplicated values are: [${uniqueDuplication.join(', ')}]`, + pass: uniqueDuplication.length !== 0, + message: () => + isNot + ? `Duplicated values are [${uniqueDuplication.join(', ')}]` + : `No duplicate values in [${received.join(', ')}]`, }; }, }); interface CustomMatchers { - toBeUnique(): void; + toContainDuplicates(): void; } declare global {