Skip to content

Commit acec1fd

Browse files
Shinigami92demipel8
authored andcommitted
test: rewrite time tests (faker-js#421)
1 parent 0bca052 commit acec1fd

File tree

1 file changed

+68
-13
lines changed

1 file changed

+68
-13
lines changed

test/time.spec.ts

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,78 @@
11
import { describe, expect, it } from 'vitest';
22
import { faker } from '../src';
33

4-
faker.seed(1234);
4+
const seededRuns = [
5+
{
6+
seed: 42,
7+
expectations: {
8+
recent: {
9+
noArgs: 'number',
10+
},
11+
},
12+
},
13+
{
14+
seed: 1337,
15+
expectations: {
16+
recent: {
17+
noArgs: 'number',
18+
},
19+
},
20+
},
21+
{
22+
seed: 1211,
23+
expectations: {
24+
recent: {
25+
noArgs: 'number',
26+
},
27+
},
28+
},
29+
];
30+
31+
const NON_SEEDED_BASED_RUN = 5;
32+
33+
const functionNames = ['recent'];
534

635
describe('time', () => {
7-
describe('recent()', () => {
8-
it('returns the recent timestamp in Unix time format', () => {
9-
const date = faker.time.recent();
10-
expect(typeof date).toBe('number');
11-
});
36+
// TODO @Shinigami92 2022-02-04: Results are not seeded yet
37+
for (const { seed, expectations } of seededRuns) {
38+
describe(`seed: ${seed}`, () => {
39+
for (const functionName of functionNames) {
40+
it(`${functionName}()`, () => {
41+
faker.seed(seed);
1242

13-
it('returns the recent timestamp in full time string format', () => {
14-
const date = faker.time.recent('wide');
15-
expect(typeof date).toBe('string');
43+
const actual = faker.time[functionName]();
44+
expect(typeof actual).toEqual(expectations[functionName].noArgs);
45+
});
46+
}
1647
});
48+
}
1749

18-
it('returns the recent timestamp in abbreviated string format', () => {
19-
const date = faker.time.recent('abbr');
20-
expect(typeof date).toBe('string');
21-
});
50+
// Create and log-back the seed for debug purposes
51+
faker.seed(Math.ceil(Math.random() * 1_000_000_000));
52+
53+
describe(`random seeded tests for seed ${faker.seedValue}`, () => {
54+
for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) {
55+
describe('recent()', () => {
56+
it('should return the recent timestamp in unix time format by default', () => {
57+
const date = faker.time.recent();
58+
expect(typeof date).toBe('number');
59+
});
60+
61+
it('should return the recent timestamp in full time string format', () => {
62+
const date = faker.time.recent('wide');
63+
expect(typeof date).toBe('string');
64+
});
65+
66+
it('should return the recent timestamp in abbreviated string format', () => {
67+
const date = faker.time.recent('abbr');
68+
expect(typeof date).toBe('string');
69+
});
70+
71+
it('should return the recent timestamp in unix time format', () => {
72+
const date = faker.time.recent('unix');
73+
expect(typeof date).toBe('number');
74+
});
75+
});
76+
}
2277
});
2378
});

0 commit comments

Comments
 (0)