1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
import type { MockInstance } from 'vitest';
import { describe, expect, it, vi } from 'vitest';
import { Faker, faker, generateMersenne32Randomizer } from '../src';
import { FakerError } from '../src/errors/faker-error';
import { keys } from '../src/internal/keys';
describe('faker', () => {
it('should not log anything on startup', async () => {
const spies: MockInstance[] = keys(console)
.filter((key) => typeof console[key] === 'function')
.map((methodName) => vi.spyOn(console, methodName));
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: Types may or may not exist, depending on whether the project was built first.
const file: unknown = await import('..');
expect(file).toBeDefined();
new Faker({ locale: { metadata: { title: '' } } });
for (const spy of spies) {
expect(spy).not.toHaveBeenCalled();
spy.mockRestore();
}
});
describe('getMetadata()', () => {
it('should return metadata for the locale', () => {
expect(faker.getMetadata()).toBeDefined();
expect(faker.getMetadata().title).toBeTypeOf('string');
// Not all properties are tested here, see locale-imports.spec.ts for full tests
});
});
describe('rawDefinitions', () => {
it('locale rawDefinition accessibility', () => {
// Metadata
expect(faker.rawDefinitions.metadata?.title).toBeDefined();
// Standard modules
expect(faker.rawDefinitions.location?.city_name).toBeDefined();
// Non-existing module
expect(faker.rawDefinitions.missing).toBeUndefined();
// Non-existing definition in a non-existing module
expect(faker.rawDefinitions.missing?.missing).toBeUndefined();
// Non-existing definition in an existing module
expect(faker.rawDefinitions.location?.missing).toBeUndefined();
});
});
describe('definitions', () => {
it('locale definition accessibility', () => {
// Metadata
expect(faker.definitions.metadata.title).toBeDefined();
// Standard modules
expect(faker.definitions.location.city_name).toBeDefined();
// Non-existing module
expect(faker.definitions.missing).toBeDefined();
// Non-existing definition in a non-existing module
expect(() => faker.definitions.missing?.missing).toThrow();
// Non-existing definition in an existing module
expect(() => faker.definitions.location.missing).toThrow();
});
});
describe('constructor()', () => {
describe('locale', () => {
it('should throw error if no locales passed', () => {
expect(() => new Faker({ locale: [] })).toThrow(
new FakerError(
'The locale option must contain at least one locale definition.'
)
);
});
});
describe('randomizer', () => {
it('should be possible to provide a custom Randomizer', () => {
const customFaker = new Faker({
locale: {},
randomizer: {
next: () => 0,
seed: () => void 0,
},
});
expect(customFaker.number.int()).toBe(0);
expect(customFaker.number.int()).toBe(0);
expect(customFaker.number.int()).toBe(0);
});
});
describe('seed', () => {
it('should be possible to provide an initial seed', () => {
const customFaker = new Faker({
locale: {},
seed: 12345,
});
expect(customFaker.number.int()).toBe(8373237378417847);
expect(customFaker.number.int()).toBe(2849657659447330);
expect(customFaker.number.int()).toBe(1656593383470774);
customFaker.seed(12345);
expect(customFaker.number.int()).toBe(8373237378417847);
expect(customFaker.number.int()).toBe(2849657659447330);
expect(customFaker.number.int()).toBe(1656593383470774);
});
});
describe('randomizer+seed', () => {
it('should take apply both the randomizer and seed', () => {
const customFaker = new Faker({
locale: {},
randomizer: generateMersenne32Randomizer(67890),
seed: 12345,
});
expect(customFaker.number.int()).toBe(8373237322874880);
expect(customFaker.number.int()).toBe(8017800868134912);
expect(customFaker.number.int()).toBe(2849657711493120);
customFaker.seed(12345); // Retry with the expected seed
expect(customFaker.number.int()).toBe(8373237322874880);
expect(customFaker.number.int()).toBe(8017800868134912);
expect(customFaker.number.int()).toBe(2849657711493120);
});
});
});
// This is only here for coverage
// The actual test is in mersenne.spec.ts
describe('seed()', () => {
it('seed()', () => {
const seed = faker.seed();
expect(seed).toBeDefined();
expect(seed).toBeTypeOf('number');
});
it('should reset the sequence when calling `seed`', () => {
const seed = faker.seed();
const num1 = faker.number.int();
const newSeed = faker.seed(seed);
const num2 = faker.number.int();
expect(num1).toBe(num2);
expect(newSeed).toBe(seed);
const num3 = faker.number.int();
expect(num1).not.toBe(num3);
});
it('seed(number)', () => {
faker.seed(1);
const actual = faker.animal.cat();
expect(actual).toBe('Korat');
});
it('seed(number[])', () => {
faker.seed([1, 2, 3]);
const actual = faker.animal.cat();
expect(actual).toBe('Oriental');
});
});
describe('defaultRefDate', () => {
it('should be a defined', () => {
expect(faker.defaultRefDate).toBeDefined();
});
it('should be a date in the very recent past', () => {
const start = Date.now();
const refDate = faker.defaultRefDate().getTime();
const end = Date.now();
expect(refDate).toBeGreaterThanOrEqual(start);
expect(refDate).toBeLessThanOrEqual(end);
});
});
});
|