Skip to content

Commit b2b7d57

Browse files
committed
test: properly enable tests for all locales
1 parent 91c93bd commit b2b7d57

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

test/all_functional.spec.ts

Lines changed: 44 additions & 43 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+
// 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,29 +69,27 @@ const modules = modulesList();
5569
describe('functional tests', () => {
5670
for (const locale in faker.locales) {
5771
describe(locale, () => {
58-
// TODO: Enable after https://github.com/faker-js/faker/pull/269
59-
//it('title', () => {
60-
// faker.locale = locale;
61-
// expect(faker.definitions.title).toBe(faker.locales[locale].title);
62-
//});
63-
6472
Object.keys(modules).forEach((module) => {
6573
describe(module, () => {
66-
// if there is nothing to test, create a dummy test so the test runner doesn't complain
67-
if (Object.keys(modules[module]).length === 0) {
68-
it.todo(`${module} was empty`);
69-
}
70-
7174
modules[module].forEach((meth) => {
72-
it(meth + '()', () => {
75+
const testAssertion = () => {
7376
faker.locale = locale;
77+
faker.seed(1);
7478
const result = faker[module][meth]();
79+
7580
if (meth === 'boolean') {
7681
expect(result).toBeTypeOf('boolean');
7782
} else {
7883
expect(result).toBeTruthy();
7984
}
80-
});
85+
};
86+
87+
if (isWorkingLocaleForMethod(module, meth, locale)) {
88+
it(meth + '()', testAssertion);
89+
} else {
90+
// TODO: Remove once there are no more failures
91+
it.fails(meth + '()', testAssertion);
92+
}
8193
});
8294
});
8395
});
@@ -88,26 +100,15 @@ describe('functional tests', () => {
88100
describe('faker.fake functional tests', () => {
89101
for (const locale in faker.locales) {
90102
describe(locale, () => {
91-
faker.locale = locale;
92-
faker.seed(1);
93103
Object.keys(modules).forEach((module) => {
94104
describe(module, () => {
95-
// if there is nothing to test, create a dummy test so the test runner doesn't complain
96-
if (Object.keys(modules[module]).length === 0) {
97-
it.todo(`${module} was empty`);
98-
}
99-
100105
modules[module].forEach((meth) => {
101106
it(meth + '()', () => {
107+
faker.locale = locale;
108+
faker.seed(1); // TODO: Use random seed once there are no more failures
102109
const result = faker.fake('{{' + module + '.' + meth + '}}');
103-
// just make sure any result is returned
104-
// an undefined result usually means an error
110+
105111
expect(result).toBeTypeOf('string');
106-
// if (meth === 'boolean') {
107-
// expect(result).toBeTypeOf('boolean');
108-
// } else {
109-
// expect(result).toBeTruthy();
110-
// }
111112
});
112113
});
113114
});

0 commit comments

Comments
 (0)