diff --git a/src/faker.ts b/src/faker.ts index d7ba3f80a44..142009e1049 100644 --- a/src/faker.ts +++ b/src/faker.ts @@ -36,7 +36,7 @@ export type UsableLocale = LiteralUnion; export type UsedLocales = Partial>; 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 54fa04aca58..283cf999e6a 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()', () => {