Skip to content

Commit 9fad09a

Browse files
authored
test: fix all_functional tests (#609)
1 parent 814880b commit 9fad09a

File tree

1 file changed

+49
-39
lines changed

1 file changed

+49
-39
lines changed

test/all_functional.spec.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ const IGNORED_MODULES = [
1111
'mersenne',
1212
];
1313

14-
const IGNORED_METHODS = {
15-
system: ['directoryPath', 'filePath'], // these are TODOs
16-
};
17-
1814
function isTestableModule(mod: string) {
1915
return IGNORED_MODULES.indexOf(mod) === -1;
2016
}
@@ -23,27 +19,45 @@ function isMethodOf(mod: string) {
2319
return (meth: string) => typeof faker[mod][meth] === 'function';
2420
}
2521

26-
function isTestableMethod(mod: string) {
27-
return (meth: string) =>
28-
!(mod in IGNORED_METHODS && IGNORED_METHODS[mod].indexOf(meth) >= 0);
29-
}
22+
const BROKEN_LOCALE_METHODS = {
23+
// TODO ST-DDT 2022-03-28: these are TODOs (usually broken locale files)
24+
address: {
25+
cityPrefix: ['pt_BR', 'pt_PT'],
26+
citySuffix: ['pt_PT'],
27+
countryCode: ['he'],
28+
state: ['az', 'cz', 'nb_NO', 'sk'],
29+
stateAbbr: ['cz', 'sk'],
30+
},
31+
company: {
32+
companySuffix: ['az'],
33+
},
34+
name: {
35+
prefix: ['az', 'id_ID', 'ru'],
36+
suffix: ['az', 'it', 'mk', 'pt_PT', 'ru'],
37+
},
38+
};
3039

31-
function both(
32-
pred1: (meth: string) => boolean,
33-
pred2: (meth: string) => boolean
34-
): (meth: string) => boolean {
35-
return (value) => pred1(value) && pred2(value);
40+
function isWorkingLocaleForMethod(
41+
mod: string,
42+
meth: string,
43+
locale: string
44+
): boolean {
45+
return (BROKEN_LOCALE_METHODS[mod]?.[meth] ?? []).indexOf(locale) === -1;
3646
}
3747

3848
// Basic smoke tests to make sure each method is at least implemented and returns a value.
3949

4050
function modulesList(): { [module: string]: string[] } {
4151
const modules = Object.keys(faker)
52+
.sort()
4253
.filter(isTestableModule)
4354
.reduce((result, mod) => {
44-
result[mod] = Object.keys(faker[mod]).filter(
45-
both(isMethodOf(mod), isTestableMethod(mod))
46-
);
55+
const methods = Object.keys(faker[mod]).filter(isMethodOf(mod));
56+
if (methods.length) {
57+
result[mod] = methods;
58+
} else {
59+
console.log(`Skipping ${mod} - No testable methods`);
60+
}
4761
return result;
4862
}, {});
4963

@@ -55,23 +69,29 @@ const modules = modulesList();
5569
describe('functional tests', () => {
5670
for (const locale in faker.locales) {
5771
describe(locale, () => {
58-
faker.locale = locale;
5972
Object.keys(modules).forEach((module) => {
6073
describe(module, () => {
61-
// if there is nothing to test, create a dummy test so the test runner doesn't complain
62-
if (Object.keys(modules[module]).length === 0) {
63-
it.todo(`${module} was empty`);
64-
}
65-
6674
modules[module].forEach((meth) => {
67-
it(meth + '()', () => {
75+
const testAssertion = () => {
76+
faker.locale = locale;
77+
// TODO ST-DDT 2022-03-28: Use random seed once there are no more failures
78+
faker.seed(1);
6879
const result = faker[module][meth]();
80+
6981
if (meth === 'boolean') {
7082
expect(result).toBeTypeOf('boolean');
7183
} else {
7284
expect(result).toBeTruthy();
7385
}
74-
});
86+
};
87+
88+
if (isWorkingLocaleForMethod(module, meth, locale)) {
89+
it(meth + '()', testAssertion);
90+
} else {
91+
// TODO ST-DDT 2022-03-28: Remove once there are no more failures
92+
// We expect a failure here to ensure we remove the exclusions when fixed
93+
it.fails(meth + '()', testAssertion);
94+
}
7595
});
7696
});
7797
});
@@ -82,26 +102,16 @@ describe('functional tests', () => {
82102
describe('faker.fake functional tests', () => {
83103
for (const locale in faker.locales) {
84104
describe(locale, () => {
85-
faker.locale = locale;
86-
faker.seed(1);
87105
Object.keys(modules).forEach((module) => {
88106
describe(module, () => {
89-
// if there is nothing to test, create a dummy test so the test runner doesn't complain
90-
if (Object.keys(modules[module]).length === 0) {
91-
it.todo(`${module} was empty`);
92-
}
93-
94107
modules[module].forEach((meth) => {
95108
it(meth + '()', () => {
109+
faker.locale = locale;
110+
// TODO ST-DDT 2022-03-28: Use random seed once there are no more failures
111+
faker.seed(1);
96112
const result = faker.fake('{{' + module + '.' + meth + '}}');
97-
// just make sure any result is returned
98-
// an undefined result usually means an error
99-
expect(result).toBeDefined();
100-
// if (meth === 'boolean') {
101-
// expect(result).toBeTypeOf('boolean');
102-
// } else {
103-
// expect(result).toBeTruthy();
104-
// }
113+
114+
expect(result).toBeTypeOf('string');
105115
});
106116
});
107117
});

0 commit comments

Comments
 (0)