aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinigami <[email protected]>2022-06-14 14:18:50 +0200
committerGitHub <[email protected]>2022-06-14 14:18:50 +0200
commit5ea8252f727e2e577c2adca9650ac8f24a171632 (patch)
treeebc4c226c2ac2d126a6e84bc43aded4d2cca2862
parente8985f60074969393fd57fc9f4f7d747742bc87c (diff)
downloadfaker-5ea8252f727e2e577c2adca9650ac8f24a171632.tar.xz
faker-5ea8252f727e2e577c2adca9650ac8f24a171632.zip
feat: throw error on unknown locale (#1071)
-rw-r--r--src/faker.ts30
-rw-r--r--test/faker.spec.ts18
2 files changed, 46 insertions, 2 deletions
diff --git a/src/faker.ts b/src/faker.ts
index d6a288c6..c84d77a4 100644
--- a/src/faker.ts
+++ b/src/faker.ts
@@ -45,8 +45,34 @@ const metadataKeys: ReadonlyArray<keyof LocaleDefinition> = [
export class Faker {
locales: UsedLocales;
- locale: UsableLocale;
- localeFallback: UsableLocale;
+ private _locale: UsableLocale;
+ private _localeFallback: UsableLocale;
+
+ get locale(): UsableLocale {
+ return this._locale;
+ }
+
+ set locale(locale: UsableLocale) {
+ if (!this.locales[locale]) {
+ throw new FakerError(
+ `Locale ${locale} is not supported. You might want to add the requested locale first to \`faker.locales\`.`
+ );
+ }
+ this._locale = locale;
+ }
+
+ get localeFallback(): UsableLocale {
+ return this._localeFallback;
+ }
+
+ set localeFallback(localeFallback: UsableLocale) {
+ if (!this.locales[localeFallback]) {
+ throw new FakerError(
+ `Locale ${localeFallback} is not supported. You might want to add the requested locale first to \`faker.locales\`.`
+ );
+ }
+ this._localeFallback = localeFallback;
+ }
readonly definitions: LocaleDefinition = this.initDefinitions();
diff --git a/test/faker.spec.ts b/test/faker.spec.ts
index 013f7a4e..0d25813b 100644
--- a/test/faker.spec.ts
+++ b/test/faker.spec.ts
@@ -32,6 +32,24 @@ describe('faker', () => {
);
});
+ it('should throw error if locale is not known', () => {
+ const instance = new Faker({ locales: { en: { title: 'English' } } });
+ expect(() => (instance.locale = 'unknown')).toThrow(
+ new FakerError(
+ 'Locale unknown is not supported. You might want to add the requested locale first to `faker.locales`.'
+ )
+ );
+ });
+
+ it('should throw error if localeFallback is not known', () => {
+ const instance = new Faker({ locales: { en: { title: 'English' } } });
+ expect(() => (instance.localeFallback = 'unknown')).toThrow(
+ new FakerError(
+ 'Locale unknown is not supported. You might want to add the requested locale first to `faker.locales`.'
+ )
+ );
+ });
+
it('should not log anything on startup', () => {
const spies: Array<SpyInstance> = Object.keys(console)
.filter((key) => typeof console[key] === 'function')