-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
all-functional.spec.ts
176 lines (154 loc) · 5.51 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
import { describe, expect, it } from 'vitest';
import type { Faker, allLocales } from '../src';
import { allFakers, fakerEN } from '../src';
import { keys } from '../src/internal/keys';
const IGNORED_MODULES = new Set([
'rawDefinitions',
'definitions',
'helpers',
'_randomizer',
'_defaultRefDate',
]);
function getMethodNamesByModules(faker: Faker): { [module: string]: string[] } {
return Object.fromEntries(
Object.keys(faker)
.filter(isTestableModule)
.sort()
.map<[string, string[]]>((moduleName) => [
moduleName,
getMethodNamesOf(faker[moduleName]),
])
.filter(([module, methods]) => {
if (methods.length === 0) {
console.log(`Skipping ${module} - No testable methods`);
return false;
}
return true;
})
);
}
function isTestableModule(moduleName: string): moduleName is keyof Faker {
return !IGNORED_MODULES.has(moduleName);
}
function getMethodNamesOf(module: object): string[] {
return keys(module).filter((method) => typeof module[method] === 'function');
}
type SkipConfig<TModule> = Partial<
Record<keyof TModule, '*' | ReadonlyArray<keyof typeof allLocales>>
>;
const BROKEN_LOCALE_METHODS = {
// TODO @ST-DDT 2022-03-28: these are TODOs (usually broken locale files)
date: {
between: '*',
betweens: '*',
},
location: {
state: ['az', 'nb_NO', 'ro_MD', 'sk'],
zipCode: ['en_HK'],
},
string: {
fromCharacters: '*',
},
person: {
prefix: ['az', 'id_ID', 'ru', 'zh_CN', 'zh_TW'],
suffix: ['az', 'it', 'mk', 'pt_PT', 'ro_MD', 'ru'],
},
} satisfies {
[module_ in keyof Faker]?: SkipConfig<Faker[module_]>;
};
function isWorkingLocaleForMethod(
module: string,
method: string,
locale: string
): boolean {
// @ts-expect-error: We don't have types for the dynamic access
const broken = BROKEN_LOCALE_METHODS[module]?.[method] ?? [];
return broken !== '*' && !broken.includes(locale);
}
// Basic smoke tests to make sure each method is at least implemented and returns a value.
const modules = getMethodNamesByModules(fakerEN);
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([]);
});
describe.each(Object.keys(modules))('%s', (module) => {
it('should not contain obsolete configuration (methods)', () => {
const existingMethods = modules[module];
const configuredMethods = Object.keys(
// @ts-expect-error: We don't have types for the dynamic access
// 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', () => {
describe.each(Object.entries(allFakers))('%s', (locale, faker) => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}
describe.each(Object.entries(modules))('%s', (module, methods) => {
// eslint-disable-next-line vitest/prefer-each -- need to dynamically succeed/fail
for (const meth of methods) {
const testAssertion = () => {
// TODO @ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
// @ts-expect-error: We don't have types for the dynamic access
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', () => {
describe.each(Object.entries(allFakers))('%s', (locale, faker) => {
if (locale === 'base') {
it.skip('base locale is checked by other tests');
return;
}
describe.each(Object.entries(modules))('%s', (module, methods) => {
// eslint-disable-next-line vitest/prefer-each -- need to dynamically succeed/fail
for (const meth of methods) {
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);
}
}
});
});
});