aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Papakostas <[email protected]>2022-03-28 09:52:27 -0700
committerGitHub <[email protected]>2022-03-28 16:52:27 +0000
commit5ed963f1e4928eb2df36f230faf7f9d63b51bef1 (patch)
tree00f5a2ba8cf2e3219454f3859041f3c76ea3339a
parent9fad09a71c1579009cb94b79e003c437a22f43a5 (diff)
downloadfaker-5ed963f1e4928eb2df36f230faf7f9d63b51bef1.tar.xz
faker-5ed963f1e4928eb2df36f230faf7f9d63b51bef1.zip
fix: force passed locales into faker constructor (#580)
Co-authored-by: Shinigami92 <[email protected]>
-rw-r--r--src/faker.ts18
-rw-r--r--test/faker.spec.ts24
2 files changed, 38 insertions, 4 deletions
diff --git a/src/faker.ts b/src/faker.ts
index d7ba3f80..142009e1 100644
--- a/src/faker.ts
+++ b/src/faker.ts
@@ -36,7 +36,7 @@ export type UsableLocale = LiteralUnion<KnownLocale>;
export type UsedLocales = Partial<Record<UsableLocale, LocaleDefinition>>;
export interface FakerOptions {
- locales?: UsedLocales;
+ locales: UsedLocales;
locale?: UsableLocale;
localeFallback?: UsableLocale;
}
@@ -81,8 +81,20 @@ export class Faker {
readonly vehicle: Vehicle = new Vehicle(this);
readonly word: Word = new Word(this);
- constructor(opts: FakerOptions = {}) {
- this.locales = this.locales || opts.locales || {};
+ constructor(opts: FakerOptions) {
+ if (!opts) {
+ throw new Error(
+ 'Options with at least one entry in locales must be provided'
+ );
+ }
+
+ if (Object.keys(opts.locales ?? {}).length === 0) {
+ throw new Error(
+ 'At least one entry in locales must be provided in the locales parameter'
+ );
+ }
+
+ this.locales = opts.locales;
this.locale = this.locale || opts.locale || 'en';
this.localeFallback = this.localeFallback || opts.localeFallback || 'en';
diff --git a/test/faker.spec.ts b/test/faker.spec.ts
index 54fa04ac..283cf999 100644
--- a/test/faker.spec.ts
+++ b/test/faker.spec.ts
@@ -1,11 +1,33 @@
import { beforeEach, describe, expect, it } from 'vitest';
-import { faker } from '../src';
+import { faker, Faker } from '../src';
describe('faker', () => {
beforeEach(() => {
faker.locale = 'en';
});
+ it('should throw error if no options passed', () => {
+ expect(
+ () =>
+ // @ts-expect-error: mission options
+ new Faker()
+ ).toThrow(
+ Error('Options with at least one entry in locales must be provided')
+ );
+ });
+
+ it('should throw error if no locales passed', () => {
+ expect(
+ () =>
+ // @ts-expect-error: missing locales
+ new Faker({})
+ ).toThrow(
+ Error(
+ 'At least one entry in locales must be provided in the locales parameter'
+ )
+ );
+ });
+
// This is only here for coverage
// The actual test is in mersenne.spec.ts
describe('seed()', () => {