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: improve animal tests #293

Merged
merged 3 commits into from
Jan 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 110 additions & 5 deletions test/animal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,116 @@
import { describe, expect, it } from 'vitest';
import { afterEach, describe, expect, it } from 'vitest';
import { faker } from '../dist/cjs';

const seededRuns = [
{
seed: 42,
expectations: {
bear: 'Sun bear',
bird: 'Iceland Gull',
cat: 'Himalayan',
cetacean: 'Pantropical Spotted Dolphin',
cow: 'Fleckvieh',
crocodilia: 'African Slender-snouted Crocodile',
dog: 'Garafian Shepherd',
fish: 'Northern snakehead',
horse: 'Furioso-North Star',
insect: 'Gouty oak gall',
lion: 'West African Lion',
rabbit: 'English Spot',
snake: 'Grey-banded kingsnake',
type: 'lion',
},
},
{
seed: 1337,
expectations: {
bear: 'Sun bear',
bird: 'American Golden-Plover',
cat: 'Devon Rex',
cetacean: 'Costero',
cow: 'Canchim',
crocodilia: 'Cuvier’s Dwarf Caiman',
dog: 'Chinese Crested Dog',
fish: 'Jumbo flying squid',
horse: 'Colorado Ranger',
insect: 'Eulophid wasp',
lion: 'Barbary Lion',
rabbit: 'Cinnamon',
snake: 'Fierce snake',
type: 'bear',
},
},
{
seed: 1211,
expectations: {
bear: 'Polar bear',
bird: 'Reed Bunting',
cat: 'Tonkinese',
cetacean: 'La Plata Dolphin',
cow: 'Breed',
crocodilia: 'Gharial',
dog: 'Tibetan Spaniel',
fish: 'Bigeye scad',
horse: 'Ukrainian Riding Horse',
insect: 'Western paper wasp',
lion: 'Cape lion',
rabbit: 'Silver Marten',
snake: 'Tiger pit viper',
type: 'horse',
},
},
];

const NON_SEEDED_BASED_RUN = 5;

const functionNames = [
'bear',
'bird',
'cat',
'cetacean',
'cow',
'crocodilia',
'dog',
'fish',
'horse',
'insect',
'lion',
'rabbit',
'snake',
'type',
];

describe('animal', () => {
describe('dog()', () => {
it('returns random value from dog array', () => {
const dog = faker.animal.dog();
expect(faker.definitions.animal.dog).toContain(dog);
afterEach(() => {
faker.locale = 'en';
});

for (let { seed, expectations } of seededRuns) {
describe(`seed: ${seed}`, () => {
for (const functionName of functionNames) {
it(`${functionName}()`, () => {
faker.seed(seed);

const actual = faker.animal[functionName]();
expect(actual).toEqual(expectations[functionName]);
});
}
});
}

// 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.seedValue}`, () => {
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);
});
});
}
}
});
});