forked from faker-js/faker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_functional.spec.ts
226 lines (203 loc) · 6.15 KB
/
all_functional.spec.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import { describe, expect, it } from 'vitest';
import type { allLocales, Faker, RandomModule } from '../src';
import { allFakers, fakerEN } from '../src';
const IGNORED_MODULES = [
'definitions',
'helpers',
'_mersenne',
'_defaultRefDate',
];
function isTestableModule(mod: string) {
return IGNORED_MODULES.indexOf(mod) === -1;
}
function isMethodOf(mod: string) {
return (meth: string) => typeof fakerEN[mod][meth] === 'function';
}
type SkipConfig<Module> = Partial<
Record<keyof Module, '*' | ReadonlyArray<keyof typeof allLocales>>
>;
const BROKEN_LOCALE_METHODS = {
// TODO @ST-DDT 2022-03-28: these are TODOs (usually broken locale files)
company: {
suffixes: ['az'],
companySuffix: ['az'],
},
location: {
state: ['az', 'nb_NO', 'sk'],
stateAbbr: ['sk'],
streetName: [
'af_ZA',
'ar',
'dv',
'el',
'en',
'en_AU',
'en_BORK',
'en_CA',
'en_GB',
'en_GH',
'en_IE',
'en_IN',
'en_NG',
'en_US',
'en_ZA',
'es',
'fa',
'fi',
'fr',
'fr_BE',
'fr_CA',
'fr_CH',
'fr_LU',
'hu',
'hy',
'id_ID',
'it',
'ja',
'ne',
'nl',
'nl_BE',
'pl',
'pt_BR',
'pt_PT',
'ur',
'vi',
'zh_CN',
'zh_TW',
'zu_ZA',
],
},
random: {
locale: '*', // locale() has been pseudo removed
} as SkipConfig<RandomModule>,
string: {
fromCharacters: '*',
},
person: {
prefix: ['az', 'id_ID', 'ru', 'zh_CN', 'zh_TW'],
suffix: ['az', 'it', 'mk', 'pt_PT', 'ru'],
jobArea: ['ar', 'fr', 'fr_BE', 'fr_CA', 'fr_CH', 'fr_LU'],
jobDescriptor: ['ar', 'fr', 'fr_BE', 'fr_CA', 'fr_CH', 'fr_LU'],
jobTitle: ['ar', 'fr', 'fr_BE', 'fr_CA', 'fr_CH', 'fr_LU', 'ur'],
jobType: ['ur'],
},
} satisfies {
[module in keyof Faker]?: SkipConfig<Faker[module]>;
};
function isWorkingLocaleForMethod(
mod: string,
meth: string,
locale: string
): boolean {
const broken = BROKEN_LOCALE_METHODS[mod]?.[meth] ?? [];
return broken !== '*' && !broken.includes(locale);
}
// Basic smoke tests to make sure each method is at least implemented and returns a value.
function modulesList(): { [module: string]: string[] } {
const modules = Object.keys(fakerEN)
.sort()
.filter(isTestableModule)
.reduce((result, mod) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const methods = Object.keys(fakerEN[mod]).filter(isMethodOf(mod));
if (methods.length) {
result[mod] = methods;
} else {
console.log(`Skipping ${mod} - No testable methods`);
}
return result;
}, {});
return modules;
}
const modules = modulesList();
describe('BROKEN_LOCALE_METHODS test', () => {
it('should not contain obsolete configuration (modules)', () => {
const existingModules = Object.keys(modules);
const configuredModules = Object.keys(BROKEN_LOCALE_METHODS ?? {});
const obsoleteModules = configuredModules.filter(
(module) => !existingModules.includes(module)
);
expect(obsoleteModules, 'No obsolete configuration').toEqual([]);
});
Object.keys(modules).forEach((module) => {
describe(module, () => {
it('should not contain obsolete configuration (methods)', () => {
const existingMethods = modules[module];
const configuredMethods = Object.keys(
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
BROKEN_LOCALE_METHODS[module] ?? {}
);
const obsoleteMethods = configuredMethods.filter(
(method) => !existingMethods.includes(method)
);
expect(obsoleteMethods, 'No obsolete configuration').toEqual([]);
});
});
});
});
describe('functional tests', () => {
for (const [locale, faker] of Object.entries(allFakers)) {
describe(locale, () => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}
Object.keys(modules).forEach((module) => {
describe(module, () => {
modules[module].forEach((meth) => {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
const result = faker[module][meth]();
if (meth === 'boolean') {
expect(result).toBeTypeOf('boolean');
} else {
expect(result).toBeTruthy();
expect(result).not.toEqual([]);
}
};
if (isWorkingLocaleForMethod(module, meth, locale)) {
it(`${meth}()`, testAssertion);
} else {
// TODO @ST-DDT 2022-03-28: Remove once there are no more failures
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
});
});
});
}
});
describe('faker.helpers.fake functional tests', () => {
for (const [locale, faker] of Object.entries(allFakers)) {
describe(locale, () => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}
Object.keys(modules).forEach((module) => {
describe(module, () => {
modules[module].forEach((meth) => {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
const result = faker.helpers.fake(`{{${module}.${meth}}}`);
expect(result).toBeTypeOf('string');
expect(result).not.toBe('');
expect(result).not.toBe('null');
expect(result).not.toBe('undefined');
};
if (isWorkingLocaleForMethod(module, meth, locale)) {
it(`${meth}()`, testAssertion);
} else {
// TODO @ST-DDT 2022-03-28: Remove once there are no more failures
// We expect a failure here to ensure we remove the exclusions when fixed
it.fails(`${meth}()`, testAssertion);
}
});
});
});
});
}
});