diff options
| author | Shinigami <[email protected]> | 2022-01-30 22:18:53 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-30 22:18:53 +0100 |
| commit | a028bfb9128bb950e9f4a3aaa04054c5d6bec5a0 (patch) | |
| tree | f4d50315b09325dd7ce00e34b4f3dbe73c9407a5 | |
| parent | 9d5a7a2533c569fa1cad2dacb6ae223644bb98cb (diff) | |
| download | faker-a028bfb9128bb950e9f4a3aaa04054c5d6bec5a0.tar.xz faker-a028bfb9128bb950e9f4a3aaa04054c5d6bec5a0.zip | |
test: rewrite unique tests (#370)
| -rw-r--r-- | test/unique.spec.ts | 143 |
1 files changed, 98 insertions, 45 deletions
diff --git a/test/unique.spec.ts b/test/unique.spec.ts index 2c312b59..5bc059d1 100644 --- a/test/unique.spec.ts +++ b/test/unique.spec.ts @@ -1,61 +1,114 @@ -import { describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it } from 'vitest'; import { faker } from '../dist/cjs'; +const seededRuns = [ + { + seed: 42, + expectations: { + withMethod: 'Test-188', + }, + }, + { + seed: 1337, + expectations: { + withMethod: 'Test-132', + }, + }, + { + seed: 1211, + expectations: { + withMethod: 'Test-465', + }, + }, +]; + +const NON_SEEDED_BASED_RUN = 5; + +const MOCK_ARRAY = Array.from( + { length: 500 }, + (_, index) => `Test-${index + 1}` +); + +function method(prefix: string = ''): string { + const element = faker.random.arrayElement(MOCK_ARRAY); + return `${prefix}${element}`; +} + describe('unique', () => { - describe('unique()', () => { - it('is able to call a function with no arguments and return a result', () => { - const result = - // @ts-expect-error - faker.unique(faker.internet.email); - expect(typeof result).toBe('string'); - }); + afterEach(() => { + faker.locale = 'en'; + }); - it('is able to call a function with arguments and return a result', () => { - const result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email - expect(result).toMatch(/\@c/); - }); + for (const { seed, expectations } of seededRuns) { + describe(`seed: ${seed}`, () => { + it(`unique(method)`, () => { + faker.seed(seed); - it('is able to call same function with arguments and return a result', () => { - const result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email - expect(result).toMatch(/\@c/); - }); + const actual = faker.unique(method); + expect(actual).toEqual(expectations.withMethod); + }); + + it(`unique(method, args)`, () => { + faker.seed(seed); - it('is able to exclude results as array', () => { - const result = faker.unique(faker.internet.protocol, [], { - exclude: ['https'], + const prefix = 'prefix-1-'; + + const actual = faker.unique(method, [prefix]); + expect(actual).toEqual(prefix + expectations.withMethod); }); - expect(result).toBe('http'); }); + } + + // Create and log-back the seed for debug purposes + faker.seed(Math.ceil(Math.random() * 1_000_000_000)); - it('is able to limit unique call by maxTime in ms', () => { - let result; - try { - result = faker.unique(faker.internet.protocol, [], { - maxTime: 1, - maxRetries: 9999, - exclude: ['https', 'http'], + describe(`random seeded tests for seed ${faker.seedValue}`, () => { + for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { + describe('unique()', () => { + it('should be possible to call a function with no arguments and return a result', () => { + const result = faker.unique(faker.internet.email); + expect(typeof result).toBe('string'); }); - } catch (err) { - expect(err.message.substr(0, 16)).toBe('Exceeded maxTime'); - } - }); - it('is able to limit unique call by maxRetries', () => { - let result; - try { - result = faker.unique(faker.internet.protocol, [], { - maxTime: 5000, - maxRetries: 5, - exclude: ['https', 'http'], + it('should be possible to call a function with arguments and return a result', () => { + const result = faker.unique(faker.internet.email, [ + 'fName', + 'lName', + 'domain', + ]); // third argument is provider, or domain for email + expect(result).toMatch(/\@domain/); }); - } catch (err) { - expect(err.message.substr(0, 19)).toBe('Exceeded maxRetries'); - } - }); - it('is able to call last function with arguments and return a result', () => { - const result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email - expect(result).toMatch(/\@c/); + it('should be possible to limit unique call by maxTime in ms', () => { + expect(() => { + faker.unique(faker.internet.protocol, [], { + maxTime: 1, + maxRetries: 9999, + exclude: ['https', 'http'], + }); + }).toThrowError(/^Exceeded maxTime:/); + }); + + it('should be possible to limit unique call by maxRetries', () => { + expect(() => { + faker.unique(faker.internet.protocol, [], { + maxTime: 5000, + maxRetries: 5, + exclude: ['https', 'http'], + }); + }).toThrowError(/^Exceeded maxRetries:/); + }); + }); + } + }); + + // This test can be only executed once, because the unique function has a global state. + // See: https://github.com/faker-js/faker/issues/371 + it('should be possible to exclude results as array', () => { + const internetProtocol = () => faker.random.arrayElement(['https', 'http']); + const result = faker.unique(internetProtocol, [], { + exclude: ['https'], }); + expect(result).toBe('http'); }); }); |
