Skip to content
Closed
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
17 changes: 16 additions & 1 deletion src/modules/internet/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,27 @@ export class InternetModule extends ModuleBase {
/**
* Generates a random domain name.
*
* @param options The optional options object.
* @param options.allowOnlyExamples When `true`, generate a domain under the IANA-reserved example domains (example.com / example.org / example.net) so the result never resolves to a real host. See issue #2228.
*
Comment on lines +543 to +545
* @example
* faker.internet.domainName() // 'slow-timer.info'
* faker.internet.domainName({ allowOnlyExamples: true }) // 'slow-timer.example.com'
*
* @since 2.0.1
*/
domainName(): string {
domainName(options?: {
/**
* When `true`, generate a domain under the IANA-reserved example domains (example.com / example.org / example.net) so the result never resolves to a real host.
*/
Comment on lines +553 to +555
allowOnlyExamples?: boolean;
}): string {
if (options?.allowOnlyExamples) {
const word = this.domainWord();
const tld = this.faker.helpers.arrayElement(['com', 'org', 'net']);
return `${word}.example.${tld}`;
}
Comment on lines +558 to +562

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be replaced by a more flexible baseDomain/parentDomain parameter.
example.com resolves to a real host, so the current claim is not really true.
We could still mention the three example domains in the jsdocs of the new parameter.

What do you think?

@matthewmayer matthewmayer Aug 11, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like

internet.domainName({baseDomain:"example.com"}) 

As it unlocks more use cases like

internet.domainName({baseDomain:"co.uk"}) 

internet.domainName({baseDomain:"yourdomain.com"}) 

internet.domainName({baseDomain:"example"}) 


return `${this.domainWord()}.${this.domainSuffix()}`;
}

Expand Down
12 changes: 9 additions & 3 deletions test/modules/__snapshots__/internet.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ exports[`internet > 42 > displayName > with firstName option 1`] = `"Jane15"`;

exports[`internet > 42 > displayName > with lastName option 1`] = `"Nikita15"`;

exports[`internet > 42 > domainName 1`] = `"hospitable-unit.net"`;
exports[`internet > 42 > domainName > noArgs 1`] = `"hospitable-unit.net"`;

exports[`internet > 42 > domainName > with allowOnlyExamples 1`] = `"hospitable-unit.example.net"`;

exports[`internet > 42 > domainSuffix 1`] = `"info"`;

Expand Down Expand Up @@ -134,7 +136,9 @@ exports[`internet > 1211 > displayName > with firstName option 1`] = `"Jane_Fahe

exports[`internet > 1211 > displayName > with lastName option 1`] = `"Dallas_Doe67"`;

exports[`internet > 1211 > domainName 1`] = `"velvety-tarragon.com"`;
exports[`internet > 1211 > domainName > noArgs 1`] = `"velvety-tarragon.com"`;

exports[`internet > 1211 > domainName > with allowOnlyExamples 1`] = `"velvety-tarragon.example.com"`;

exports[`internet > 1211 > domainSuffix 1`] = `"org"`;

Expand Down Expand Up @@ -252,7 +256,9 @@ exports[`internet > 1337 > displayName > with firstName option 1`] = `"Jane.Gott

exports[`internet > 1337 > displayName > with lastName option 1`] = `"Elda.Doe"`;

exports[`internet > 1337 > domainName 1`] = `"fatal-co-producer.com"`;
exports[`internet > 1337 > domainName > noArgs 1`] = `"fatal-co-producer.com"`;

exports[`internet > 1337 > domainName > with allowOnlyExamples 1`] = `"fatal-co-producer.example.com"`;

exports[`internet > 1337 > domainSuffix 1`] = `"com"`;

Expand Down
4 changes: 3 additions & 1 deletion test/modules/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ describe('internet', () => {
t.itEach(
'protocol',
'httpMethod',
'domainName',
'domainSuffix',
'domainWord',
'ip',
Expand All @@ -36,6 +35,9 @@ describe('internet', () => {
'port',
'userAgent'
);
t.describe('domainName', (t) => {
t.it('noArgs').it('with allowOnlyExamples', { allowOnlyExamples: true });
});

t.describe('email', (t) => {
t.it('noArgs')
Expand Down