Skip to content

Commit

Permalink
test: vitest prefer-each (#2144)
Browse files Browse the repository at this point in the history
Co-authored-by: ST-DDT <ST-DDT@gmx.de>
  • Loading branch information
Shinigami92 and ST-DDT committed May 20, 2023
1 parent 06bc5af commit 614fd67
Show file tree
Hide file tree
Showing 31 changed files with 433 additions and 406 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -128,6 +128,7 @@ module.exports = defineConfig({
],

'vitest/expect-expect': 'off',
'vitest/prefer-each': 'error',
'vitest/valid-expect': ['error', { maxArgs: 2 }],
},
},
Expand Down
8 changes: 5 additions & 3 deletions test/airline.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { Aircraft, faker } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

Expand Down Expand Up @@ -40,8 +41,9 @@ describe('airline', () => {
});
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe(`airport()`, () => {
it('should return a random value from airport array', () => {
const airport = faker.airline.airport();
Expand Down Expand Up @@ -175,5 +177,5 @@ describe('airline', () => {
});
});
}
});
);
});
146 changes: 68 additions & 78 deletions test/all_functional.spec.ts
Expand Up @@ -137,94 +137,84 @@ describe('BROKEN_LOCALE_METHODS test', () => {
expect(obsoleteModules, 'No obsolete configuration').toEqual([]);
});

Object.keys(modules).forEach((module) => {
describe(`${module}`, () => {
it('should not contain obsolete configuration (methods)', () => {
const existingMethods = modules[module];
const configuredMethods = Object.keys(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
BROKEN_LOCALE_METHODS[module] ?? {}
);
const obsoleteMethods = configuredMethods.filter(
(method) => !existingMethods.includes(method)
);

expect(obsoleteMethods, 'No obsolete configuration').toEqual([]);
});
describe.each(Object.keys(modules))('%s', (module) => {
it('should not contain obsolete configuration (methods)', () => {
const existingMethods = modules[module];
const configuredMethods = Object.keys(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
BROKEN_LOCALE_METHODS[module] ?? {}
);
const obsoleteMethods = configuredMethods.filter(
(method) => !existingMethods.includes(method)
);

expect(obsoleteMethods, 'No obsolete configuration').toEqual([]);
});
});
});

describe('functional tests', () => {
for (const [locale, faker] of Object.entries(allFakers)) {
describe(`${locale}`, () => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}

Object.keys(modules).forEach((module) => {
describe(`${module}`, () => {
modules[module].forEach((meth) => {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
const result = faker[module][meth]();

if (meth === 'boolean') {
expect(result).toBeTypeOf('boolean');
} else {
expect(result).toBeTruthy();
expect(result).not.toEqual([]);
}
};

if (isWorkingLocaleForMethod(module, meth, locale)) {
it(`${meth}()`, testAssertion);
} else {
// TODO @ST-DDT 2022-03-28: Remove once there are no more failures
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
});
describe.each(Object.entries(allFakers))('%s', (locale, faker) => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}

describe.each(Object.entries(modules))('%s', (module, methods) => {
methods.forEach((meth) => {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
const result = faker[module][meth]();

if (meth === 'boolean') {
expect(result).toBeTypeOf('boolean');
} else {
expect(result).toBeTruthy();
expect(result).not.toEqual([]);
}
};

if (isWorkingLocaleForMethod(module, meth, locale)) {
it(`${meth}()`, testAssertion);
} else {
// TODO @ST-DDT 2022-03-28: Remove once there are no more failures
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
});
}
});
});

describe('faker.helpers.fake functional tests', () => {
for (const [locale, faker] of Object.entries(allFakers)) {
describe(`${locale}`, () => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}

Object.keys(modules).forEach((module) => {
describe(`${module}`, () => {
modules[module].forEach((meth) => {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
const result = faker.helpers.fake(`{{${module}.${meth}}}`);

expect(result).toBeTypeOf('string');
expect(result).not.toBe('');
expect(result).not.toBe('null');
expect(result).not.toBe('undefined');
};

if (isWorkingLocaleForMethod(module, meth, locale)) {
it(`${meth}()`, testAssertion);
} else {
// TODO @ST-DDT 2022-03-28: Remove once there are no more failures
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
});
describe.each(Object.entries(allFakers))('%s', (locale, faker) => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}

describe.each(Object.entries(modules))('%s', (module, methods) => {
methods.forEach((meth) => {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
const result = faker.helpers.fake(`{{${module}.${meth}}}`);

expect(result).toBeTypeOf('string');
expect(result).not.toBe('');
expect(result).not.toBe('null');
expect(result).not.toBe('undefined');
};

if (isWorkingLocaleForMethod(module, meth, locale)) {
it(`${meth}()`, testAssertion);
} else {
// TODO @ST-DDT 2022-03-28: Remove once there are no more failures
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
});
}
});
});
20 changes: 10 additions & 10 deletions test/animal.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

Expand All @@ -27,16 +28,15 @@ describe('animal', () => {
t.itEach(...functionNames);
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
for (const functionName of functionNames) {
describe(`${functionName}()`, () => {
it(`should return random value from ${functionName} array`, () => {
const actual = faker.animal[functionName]();
expect(faker.definitions.animal[functionName]).toContain(actual);
});
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe.each(functionNames)('%s()', (functionName) => {
it(`should return random value from ${functionName} array`, () => {
const actual = faker.animal[functionName]();
expect(faker.definitions.animal[functionName]).toContain(actual);
});
}
});
}
});
);
});
8 changes: 5 additions & 3 deletions test/color.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { CssFunction, CssSpace, faker } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

Expand All @@ -24,8 +25,9 @@ describe('color', () => {
// Create and log-back the seed for debug purposes
faker.seed(Math.ceil(Math.random() * 1_000_000_000));

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe(`human()`, () => {
it('should return random human readable color from human color array', () => {
const color = faker.color.human();
Expand Down Expand Up @@ -403,5 +405,5 @@ describe('color', () => {
});
});
}
});
);
});
8 changes: 5 additions & 3 deletions test/commerce.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

Expand Down Expand Up @@ -39,8 +40,9 @@ describe('commerce', () => {
});
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe(`department()`, () => {
it('should return random value from department array', () => {
const department = faker.commerce.department();
Expand Down Expand Up @@ -157,5 +159,5 @@ describe('commerce', () => {
});
});
}
});
);
});
8 changes: 5 additions & 3 deletions test/company.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

Expand All @@ -23,8 +24,9 @@ describe('company', () => {
t.skip('bs').skip('bsAdjective').skip('bsBuzz').skip('bsNoun');
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe('suffixes()', () => {
it('should return all suffixes', () => {
const actual = faker.company.suffixes();
Expand Down Expand Up @@ -139,5 +141,5 @@ describe('company', () => {
});
});
}
});
);
});
8 changes: 5 additions & 3 deletions test/database.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

Expand All @@ -9,8 +10,9 @@ describe('database', () => {
t.itEach('column', 'type', 'collation', 'engine', 'mongodbObjectId');
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe('column()', () => {
it('should return a column name from array', () => {
const column = faker.database.column();
Expand Down Expand Up @@ -54,5 +56,5 @@ describe('database', () => {
});
});
}
});
);
});
8 changes: 5 additions & 3 deletions test/datatype.spec.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest';
import { faker, FakerError } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 25;

Expand Down Expand Up @@ -89,8 +90,9 @@ describe('datatype', () => {
});
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe('number', () => {
it('should return a random number given a maximum value as Number', () => {
const max = 10;
Expand Down Expand Up @@ -548,5 +550,5 @@ describe('datatype', () => {
});
});
}
});
);
});
8 changes: 5 additions & 3 deletions test/date.spec.ts
@@ -1,6 +1,7 @@
import { afterEach, describe, expect, it } from 'vitest';
import { faker, fakerAZ, FakerError } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const converterMap = [
(d: Date) => d,
Expand Down Expand Up @@ -200,8 +201,9 @@ describe('date', () => {
});
});

describe(`random seeded tests for seed ${faker.seed()}`, () => {
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe('anytime()', () => {
it('should return a date', () => {
const actual = faker.date.anytime();
Expand Down Expand Up @@ -788,7 +790,7 @@ describe('date', () => {
});
});
}
});
);

describe('refDateSource', () => {
afterEach(() => {
Expand Down

0 comments on commit 614fd67

Please sign in to comment.