-
-
Notifications
You must be signed in to change notification settings - Fork 929
/
all_functional.spec.ts
121 lines (107 loc) · 3.26 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
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
const IGNORED_MODULES = [
'locales',
'locale',
'localeFallback',
'definitions',
'fake',
'helpers',
'mersenne',
];
function isTestableModule(mod: string) {
return IGNORED_MODULES.indexOf(mod) === -1;
}
function isMethodOf(mod: string) {
return (meth: string) => typeof faker[mod][meth] === 'function';
}
const BROKEN_LOCALE_METHODS = {
// TODO ST-DDT 2022-03-28: these are TODOs (usually broken locale files)
address: {
cityPrefix: ['pt_BR', 'pt_PT'],
citySuffix: ['pt_PT'],
state: ['az', 'cz', 'nb_NO', 'sk'],
stateAbbr: ['cz', 'sk'],
},
company: {
companySuffix: ['az'],
},
name: {
prefix: ['az', 'id_ID', 'ru'],
suffix: ['az', 'it', 'mk', 'pt_PT', 'ru'],
},
};
function isWorkingLocaleForMethod(
mod: string,
meth: string,
locale: string
): boolean {
return (BROKEN_LOCALE_METHODS[mod]?.[meth] ?? []).indexOf(locale) === -1;
}
// 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(faker)
.sort()
.filter(isTestableModule)
.reduce((result, mod) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const methods = Object.keys(faker[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('functional tests', () => {
for (const locale in faker.locales) {
describe(locale, () => {
Object.keys(modules).forEach((module) => {
describe(module, () => {
modules[module].forEach((meth) => {
const testAssertion = () => {
faker.locale = locale;
// 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();
}
};
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.fake functional tests', () => {
for (const locale in faker.locales) {
describe(locale, () => {
Object.keys(modules).forEach((module) => {
describe(module, () => {
modules[module].forEach((meth) => {
it(`${meth}()`, () => {
faker.locale = locale;
// TODO ST-DDT 2022-03-28: Use random seed once there are no more failures
faker.seed(1);
const result = faker.fake(`{{${module}.${meth}}}`);
expect(result).toBeTypeOf('string');
});
});
});
});
});
}
});