Skip to content

Commit 9ecdf8a

Browse files
Shinigami92demipel8
authored andcommitted
test: rewrite unique tests (#370)
1 parent 87e734c commit 9ecdf8a

File tree

1 file changed

+98
-45
lines changed

1 file changed

+98
-45
lines changed

test/unique.spec.ts

Lines changed: 98 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,114 @@
1-
import { describe, expect, it } from 'vitest';
1+
import { afterEach, describe, expect, it } from 'vitest';
22
import { faker } from '../dist/cjs';
33

4+
const seededRuns = [
5+
{
6+
seed: 42,
7+
expectations: {
8+
withMethod: 'Test-188',
9+
},
10+
},
11+
{
12+
seed: 1337,
13+
expectations: {
14+
withMethod: 'Test-132',
15+
},
16+
},
17+
{
18+
seed: 1211,
19+
expectations: {
20+
withMethod: 'Test-465',
21+
},
22+
},
23+
];
24+
25+
const NON_SEEDED_BASED_RUN = 5;
26+
27+
const MOCK_ARRAY = Array.from(
28+
{ length: 500 },
29+
(_, index) => `Test-${index + 1}`
30+
);
31+
32+
function method(prefix: string = ''): string {
33+
const element = faker.random.arrayElement(MOCK_ARRAY);
34+
return `${prefix}${element}`;
35+
}
36+
437
describe('unique', () => {
5-
describe('unique()', () => {
6-
it('is able to call a function with no arguments and return a result', () => {
7-
const result =
8-
// @ts-expect-error
9-
faker.unique(faker.internet.email);
10-
expect(typeof result).toBe('string');
11-
});
38+
afterEach(() => {
39+
faker.locale = 'en';
40+
});
1241

13-
it('is able to call a function with arguments and return a result', () => {
14-
const result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
15-
expect(result).toMatch(/\@c/);
16-
});
42+
for (const { seed, expectations } of seededRuns) {
43+
describe(`seed: ${seed}`, () => {
44+
it(`unique(method)`, () => {
45+
faker.seed(seed);
1746

18-
it('is able to call same function with arguments and return a result', () => {
19-
const result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
20-
expect(result).toMatch(/\@c/);
21-
});
47+
const actual = faker.unique(method);
48+
expect(actual).toEqual(expectations.withMethod);
49+
});
50+
51+
it(`unique(method, args)`, () => {
52+
faker.seed(seed);
2253

23-
it('is able to exclude results as array', () => {
24-
const result = faker.unique(faker.internet.protocol, [], {
25-
exclude: ['https'],
54+
const prefix = 'prefix-1-';
55+
56+
const actual = faker.unique(method, [prefix]);
57+
expect(actual).toEqual(prefix + expectations.withMethod);
2658
});
27-
expect(result).toBe('http');
2859
});
60+
}
61+
62+
// Create and log-back the seed for debug purposes
63+
faker.seed(Math.ceil(Math.random() * 1_000_000_000));
2964

30-
it('is able to limit unique call by maxTime in ms', () => {
31-
let result;
32-
try {
33-
result = faker.unique(faker.internet.protocol, [], {
34-
maxTime: 1,
35-
maxRetries: 9999,
36-
exclude: ['https', 'http'],
65+
describe(`random seeded tests for seed ${faker.seedValue}`, () => {
66+
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
67+
describe('unique()', () => {
68+
it('should be possible to call a function with no arguments and return a result', () => {
69+
const result = faker.unique(faker.internet.email);
70+
expect(typeof result).toBe('string');
3771
});
38-
} catch (err) {
39-
expect(err.message.substr(0, 16)).toBe('Exceeded maxTime');
40-
}
41-
});
4272

43-
it('is able to limit unique call by maxRetries', () => {
44-
let result;
45-
try {
46-
result = faker.unique(faker.internet.protocol, [], {
47-
maxTime: 5000,
48-
maxRetries: 5,
49-
exclude: ['https', 'http'],
73+
it('should be possible to call a function with arguments and return a result', () => {
74+
const result = faker.unique(faker.internet.email, [
75+
'fName',
76+
'lName',
77+
'domain',
78+
]); // third argument is provider, or domain for email
79+
expect(result).toMatch(/\@domain/);
5080
});
51-
} catch (err) {
52-
expect(err.message.substr(0, 19)).toBe('Exceeded maxRetries');
53-
}
54-
});
5581

56-
it('is able to call last function with arguments and return a result', () => {
57-
const result = faker.unique(faker.internet.email, ['a', 'b', 'c']); // third argument is provider, or domain for email
58-
expect(result).toMatch(/\@c/);
82+
it('should be possible to limit unique call by maxTime in ms', () => {
83+
expect(() => {
84+
faker.unique(faker.internet.protocol, [], {
85+
maxTime: 1,
86+
maxRetries: 9999,
87+
exclude: ['https', 'http'],
88+
});
89+
}).toThrowError(/^Exceeded maxTime:/);
90+
});
91+
92+
it('should be possible to limit unique call by maxRetries', () => {
93+
expect(() => {
94+
faker.unique(faker.internet.protocol, [], {
95+
maxTime: 5000,
96+
maxRetries: 5,
97+
exclude: ['https', 'http'],
98+
});
99+
}).toThrowError(/^Exceeded maxRetries:/);
100+
});
101+
});
102+
}
103+
});
104+
105+
// This test can be only executed once, because the unique function has a global state.
106+
// See: https://github.com/faker-js/faker/issues/371
107+
it('should be possible to exclude results as array', () => {
108+
const internetProtocol = () => faker.random.arrayElement(['https', 'http']);
109+
const result = faker.unique(internetProtocol, [], {
110+
exclude: ['https'],
59111
});
112+
expect(result).toBe('http');
60113
});
61114
});

0 commit comments

Comments
 (0)