Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/modules/system/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FakerError } from '../../errors/faker-error';
import { ModuleBase } from '../../internal/module-base';

const commonFileTypes = ['video', 'audio', 'image', 'text', 'application'];
Expand Down Expand Up @@ -175,7 +176,12 @@ export class SystemModule extends ModuleBase {
const mimeTypes = this.faker.definitions.system.mime_type;

if (typeof mimeType === 'string') {
return this.faker.helpers.arrayElement(mimeTypes[mimeType].extensions);
const entry = mimeTypes[mimeType];
if (entry == null) {
throw new FakerError(`MIME type ${mimeType} is not supported.`);
}

return this.faker.helpers.arrayElement(entry.extensions);
}

const extensionSet = new Set(
Expand Down
8 changes: 7 additions & 1 deletion test/modules/system.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isMimeType, isSemVer } from 'validator';
import { describe, expect, it } from 'vitest';
import { faker } from '../../src';
import { FakerError, faker } from '../../src';
import { seededTests } from '../support/seeded-runs';
import { times } from './../support/times';

Expand Down Expand Up @@ -190,6 +190,12 @@ describe('system', () => {
)}]. Got "${actual}".`
).include(actual);
});

it('should throw for unsupported mimeType', () => {
expect(() => faker.system.fileExt('application/not-real')).toThrow(
new FakerError('MIME type application/not-real is not supported.')
);
});
});

describe('fileName()', () => {
Expand Down
Loading