From 29d39ed149abc08bc968e5b0b137826c387f1e34 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 17:05:01 +0200 Subject: [PATCH 01/11] feat(helpers): fake --- src/modules/helpers/index.ts | 131 +++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index a68b66ae9de..445e74c2bdd 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { FakerError } from '../../errors/faker-error'; import { luhnCheckValue } from './luhn-check'; /** @@ -454,4 +455,134 @@ export class Helpers { return arrayCopy.slice(min); } + + /** + * Generator for combining faker methods based on a static string input. + * + * Note: We recommend using string template literals instead of `fake()`, + * which are faster and strongly typed (if you are using TypeScript), + * e.g. ``const address = `${faker.address.zipCode()} ${faker.address.city()}`;`` + * + * This method is useful if you have to build a random string from a static, non-executable source + * (e.g. string coming from a user, stored in a database or a file). + * + * It checks the given string for placeholders and replaces them by calling faker methods: + * + * ```js + * const hello = faker.fake('Hi, my name is {{name.firstName}} {{name.lastName}}!') + * ``` + * + * This would use the `faker.name.firstName()` and `faker.name.lastName()` method to resolve the placeholders respectively. + * + * It is also possible to provide parameters. At first, they will be parsed as json, + * and if that isn't possible, we will fall back to string: + * + * ```js + * const message = faker.fake(`You can call me at {{phone.number(+!# !## #### #####!)}}.') + * ``` + * + * Currently it is not possible to set more than a single parameter. + * + * It is also NOT possible to use any non-faker methods or plain javascript in such templates. + * + * @param str The template string that will get interpolated. Must not be empty. + * + * @see faker.helpers.mustache() to use custom functions for resolution. + * + * @example + * faker.fake('{{name.lastName}}') // 'Barrows' + * faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}') // 'Durgan, Noe MD' + * faker.fake('This is static test.') // 'This is static test.' + * faker.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!' + * faker.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.' + * faker.fake('I flipped the coin an got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails' + */ + fake(str: string): string { + // if incoming str parameter is not provided, return error message + if (typeof str !== 'string' || str.length === 0) { + throw new FakerError('string parameter is required!'); + } + + // find first matching {{ and }} + const start = str.search(/{{[a-z]/); + const end = str.indexOf('}}', start); + + // if no {{ and }} is found, we are done + if (start === -1 || end === -1) { + return str; + } + + // extract method name from between the {{ }} that we found + // for example: {{name.firstName}} + const token = str.substring(start + 2, end + 2); + let method = token.replace('}}', '').replace('{{', ''); + + // extract method parameters + const regExp = /\(([^)]+)\)/; + const matches = regExp.exec(method); + let parameters = ''; + if (matches) { + method = method.replace(regExp, ''); + parameters = matches[1]; + } + + // split the method into module and function + const parts = method.split('.'); + + let currentModuleOrMethod: unknown = this.faker; + let currentDefinitions: unknown = this.faker.definitions; + + // Search for the requested method or definition + for (const part of parts) { + currentModuleOrMethod = currentModuleOrMethod?.[part]; + currentDefinitions = currentDefinitions?.[part]; + } + + // Make method executable + let fn: (args?: unknown) => unknown; + if (typeof currentModuleOrMethod === 'function') { + fn = currentModuleOrMethod as (args?: unknown) => unknown; + } else if (Array.isArray(currentDefinitions)) { + fn = () => + this.faker.helpers.arrayElement(currentDefinitions as unknown[]); + } else { + throw new FakerError(`Invalid module method or definition: ${method} +- faker.${method} is not a function +- faker.definitions.${method} is not an array`); + } + + // assign the function from the module.function namespace + fn = fn.bind(this); + + // If parameters are populated here, they are always going to be of string type + // since we might actually be dealing with an object or array, + // we always attempt to the parse the incoming parameters into JSON + let params: unknown; + // Note: we experience a small performance hit here due to JSON.parse try / catch + // If anyone actually needs to optimize this specific code path, please open a support issue on github + try { + params = JSON.parse(parameters); + } catch (err) { + // since JSON.parse threw an error, assume parameters was actually a string + params = parameters; + } + + let result: string; + if (typeof params === 'string' && params.length === 0) { + result = String(fn()); + } else { + result = String(fn(params)); + } + + // Replace the found tag with the returned fake value + // We cannot use string.replace here because the result might contain evaluated characters + const res = str.substring(0, start) + result + str.substring(end + 2); + + if (res === '') { + return ''; + } + + // return the response recursively until we are done finding all tags + return this.fake(res); + } } From a83a34cbaa9b4e9160d2fdccdf798d0450e323ad Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 17:11:53 +0200 Subject: [PATCH 02/11] refactor(fake): deprecate fake --- src/modules/address/index.ts | 6 +-- src/modules/company/index.ts | 14 +++--- src/modules/fake/index.ts | 98 ++++-------------------------------- 3 files changed, 21 insertions(+), 97 deletions(-) diff --git a/src/modules/address/index.ts b/src/modules/address/index.ts index 7c55bb51325..4c2469e1b7e 100644 --- a/src/modules/address/index.ts +++ b/src/modules/address/index.ts @@ -86,7 +86,7 @@ export class Address { format = this.faker.datatype.number(formats.length - 1); } - return this.faker.fake(formats[format]); + return this.faker.helpers.fake(formats[format]); } /** @@ -171,7 +171,7 @@ export class Address { const format = this.faker.helpers.arrayElement( this.faker.definitions.address.street ); - return this.faker.fake(format); + return this.faker.helpers.fake(format); } /** @@ -212,7 +212,7 @@ export class Address { const formats = this.faker.definitions.address.street_address; const format = formats[useFullAddress ? 'full' : 'normal']; - return this.faker.fake(format); + return this.faker.helpers.fake(format); } /** diff --git a/src/modules/company/index.ts b/src/modules/company/index.ts index 968afbbfcbe..1dce91fd935 100644 --- a/src/modules/company/index.ts +++ b/src/modules/company/index.ts @@ -45,7 +45,7 @@ export class Company { format = this.faker.datatype.number(formats.length - 1); } - return this.faker.fake(formats[format]); + return this.faker.helpers.fake(formats[format]); } /** @@ -88,9 +88,11 @@ export class Company { * faker.company.catchPhrase() // 'Upgradable systematic flexibility' */ catchPhrase(): string { - return this.faker.fake( - '{{company.catchPhraseAdjective}} {{company.catchPhraseDescriptor}} {{company.catchPhraseNoun}}' - ); + return [ + this.catchPhraseAdjective(), + this.catchPhraseDescriptor(), + this.catchPhraseNoun(), + ].join(' '); } /** @@ -100,9 +102,7 @@ export class Company { * faker.company.bs() // 'cultivate synergistic e-markets' */ bs(): string { - return this.faker.fake( - '{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}' - ); + return [this.bsBuzz(), this.bsAdjective(), this.bsNoun()].join(' '); } /** diff --git a/src/modules/fake/index.ts b/src/modules/fake/index.ts index 9dff1c8ec36..9926f435987 100644 --- a/src/modules/fake/index.ts +++ b/src/modules/fake/index.ts @@ -1,5 +1,5 @@ import type { Faker } from '../..'; -import { FakerError } from '../../errors/faker-error'; +import { deprecated } from '../../internal/deprecated'; /** * Generator method for combining faker methods based on string input. @@ -47,6 +47,7 @@ export class Fake { * @param str The template string that will get interpolated. Must not be empty. * * @see faker.helpers.mustache() to use custom functions for resolution. + * @see faker.helpers.fake() * * @example * faker.fake('{{name.lastName}}') // 'Barrows' @@ -55,93 +56,16 @@ export class Fake { * faker.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!' * faker.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.' * faker.fake('I flipped the coin an got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails' + * + * @deprecated Use faker.helpers.fake() instead. */ fake(str: string): string { - // if incoming str parameter is not provided, return error message - if (typeof str !== 'string' || str.length === 0) { - throw new FakerError('string parameter is required!'); - } - - // find first matching {{ and }} - const start = str.search(/{{[a-z]/); - const end = str.indexOf('}}', start); - - // if no {{ and }} is found, we are done - if (start === -1 || end === -1) { - return str; - } - - // extract method name from between the {{ }} that we found - // for example: {{name.firstName}} - const token = str.substring(start + 2, end + 2); - let method = token.replace('}}', '').replace('{{', ''); - - // extract method parameters - const regExp = /\(([^)]+)\)/; - const matches = regExp.exec(method); - let parameters = ''; - if (matches) { - method = method.replace(regExp, ''); - parameters = matches[1]; - } - - // split the method into module and function - const parts = method.split('.'); - - let currentModuleOrMethod: unknown = this.faker; - let currentDefinitions: unknown = this.faker.definitions; - - // Search for the requested method or definition - for (const part of parts) { - currentModuleOrMethod = currentModuleOrMethod?.[part]; - currentDefinitions = currentDefinitions?.[part]; - } - - // Make method executable - let fn: (args?: unknown) => unknown; - if (typeof currentModuleOrMethod === 'function') { - fn = currentModuleOrMethod as (args?: unknown) => unknown; - } else if (Array.isArray(currentDefinitions)) { - fn = () => - this.faker.helpers.arrayElement(currentDefinitions as unknown[]); - } else { - throw new FakerError(`Invalid module method or definition: ${method} -- faker.${method} is not a function -- faker.definitions.${method} is not an array`); - } - - // assign the function from the module.function namespace - fn = fn.bind(this); - - // If parameters are populated here, they are always going to be of string type - // since we might actually be dealing with an object or array, - // we always attempt to the parse the incoming parameters into JSON - let params: unknown; - // Note: we experience a small performance hit here due to JSON.parse try / catch - // If anyone actually needs to optimize this specific code path, please open a support issue on github - try { - params = JSON.parse(parameters); - } catch (err) { - // since JSON.parse threw an error, assume parameters was actually a string - params = parameters; - } - - let result: string; - if (typeof params === 'string' && params.length === 0) { - result = String(fn()); - } else { - result = String(fn(params)); - } - - // Replace the found tag with the returned fake value - // We cannot use string.replace here because the result might contain evaluated characters - const res = str.substring(0, start) + result + str.substring(end + 2); - - if (res === '') { - return ''; - } - - // return the response recursively until we are done finding all tags - return this.fake(res); + deprecated({ + deprecated: 'faker.fake()', + proposed: 'faker.helpers.fake()', + since: '7.4', + until: '8.0', + }); + return this.faker.helpers.fake(str); } } From 909501ab69a3703cbd79d8233ab8ce96628c474c Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 17:13:49 +0200 Subject: [PATCH 03/11] test(helpers): copy test of faker.fake --- test/helpers.spec.ts | 126 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 6a89667f774..c15fe6c2bd5 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it } from 'vitest'; -import { faker } from '../src'; +import { faker, FakerError } from '../src'; import { luhnCheck } from '../src/modules/helpers/luhn-check'; import { seededTests } from './support/seededRuns'; @@ -477,6 +477,130 @@ describe('helpers', () => { expect(actual).toBeUndefined(); }); }); + + describe('fake()', () => { + it('replaces a token with a random value for a method with no parameters', () => { + const name = faker.helpers.fake('{{phone.number}}'); + expect(name).toMatch(/\d/); + }); + + it('replaces multiple tokens with random values for methods with no parameters', () => { + const name = faker.helpers.fake( + '{{helpers.arrayElement}}{{helpers.arrayElement}}{{helpers.arrayElement}}' + ); + expect(name).toMatch(/[abc]{3}/); + }); + + it('replaces a token with a random value for a methods with a simple parameter', () => { + const random = faker.helpers.fake( + '{{helpers.slugify("Will This Work")}}' + ); + expect(random).toBe('Will-This-Work'); + }); + + it('replaces a token with a random value for a method with an array parameter', () => { + const arr = ['one', 'two', 'three']; + const random = faker.helpers.fake( + '{{helpers.arrayElement(["one", "two", "three"])}}' + ); + expect(arr).toContain(random); + }); + + it('does not allow undefined parameters', () => { + expect(() => + // @ts-expect-error: The parameter is required + faker.helpers.fake() + ).toThrowError(new FakerError('string parameter is required!')); + }); + + it('does not allow invalid module name', () => { + expect(() => faker.helpers.fake('{{foo.bar}}')).toThrowError( + new FakerError(`Invalid module method or definition: foo.bar +- faker.foo.bar is not a function +- faker.definitions.foo.bar is not an array`) + ); + }); + + it('does not allow missing method name', () => { + expect(() => faker.helpers.fake('{{address}}')).toThrowError( + new FakerError(`Invalid module method or definition: address +- faker.address is not a function +- faker.definitions.address is not an array`) + ); + }); + + it('does not allow invalid method name', () => { + expect(() => faker.helpers.fake('{{address.foo}}')).toThrowError( + new FakerError(`Invalid module method or definition: address.foo +- faker.address.foo is not a function +- faker.definitions.address.foo is not an array`) + ); + }); + + it('does not allow invalid definitions data', () => { + expect(() => + faker.helpers.fake('{{finance.credit_card}}') + ).toThrowError( + new FakerError(`Invalid module method or definition: finance.credit_card +- faker.finance.credit_card is not a function +- faker.definitions.finance.credit_card is not an array`) + ); + }); + + it('should be able to return empty strings', () => { + expect(faker.helpers.fake('{{helpers.repeatString}}')).toBe(''); + }); + + it('should be able to return locale definition strings', () => { + expect(faker.definitions.cell_phone.formats).toContain( + faker.helpers.fake('{{cell_phone.formats}}') + ); + }); + + it('should be able to return locale definition strings that starts with the name of an existing module', () => { + expect(faker.definitions.address.city_name).toContain( + faker.helpers.fake('{{address.city_name}}') + ); + }); + + it('should be able to handle only {{ brackets', () => { + expect(faker.helpers.fake('{{hello')).toBe('{{hello'); + expect(faker.helpers.fake('hello{{')).toBe('hello{{'); + }); + + it('should be able to handle only }} brackets', () => { + expect(faker.helpers.fake('hello}}')).toBe('hello}}'); + expect(faker.helpers.fake('}}hello')).toBe('}}hello'); + }); + + it('should be able to handle reverted brackets', () => { + expect(faker.helpers.fake('}}hello{{')).toBe('}}hello{{'); + }); + + it('should be able to handle random }} brackets', () => { + expect(faker.helpers.fake('}}hello{{random.alpha}}')).toMatch( + /^}}hello[a-z]$/ + ); + }); + + it('should be able to handle connected brackets', () => { + expect(faker.helpers.fake('{{{random.alpha}}}')).toMatch(/^{[a-z]}$/); + }); + + it('should be able to handle empty brackets', () => { + expect(faker.helpers.fake('{{}}')).toBe('{{}}'); + }); + + it('should be able to handle special replacement patterns', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (faker.random as any).special = () => '$&'; + + expect(faker.helpers.fake('{{random.special}}')).toBe('$&'); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + delete (faker.random as any).special; + }); + }); } }); }); From 5a49fe34f8658420ff78f22634d3db1afe21caa6 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 17:24:05 +0200 Subject: [PATCH 04/11] docs(helpers): update fake JSDocs --- src/modules/helpers/index.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/modules/helpers/index.ts b/src/modules/helpers/index.ts index 445e74c2bdd..b4d73b2e934 100644 --- a/src/modules/helpers/index.ts +++ b/src/modules/helpers/index.ts @@ -469,7 +469,7 @@ export class Helpers { * It checks the given string for placeholders and replaces them by calling faker methods: * * ```js - * const hello = faker.fake('Hi, my name is {{name.firstName}} {{name.lastName}}!') + * const hello = faker.helpers.fake('Hi, my name is {{name.firstName}} {{name.lastName}}!') * ``` * * This would use the `faker.name.firstName()` and `faker.name.lastName()` method to resolve the placeholders respectively. @@ -478,7 +478,7 @@ export class Helpers { * and if that isn't possible, we will fall back to string: * * ```js - * const message = faker.fake(`You can call me at {{phone.number(+!# !## #### #####!)}}.') + * const message = faker.helpers.fake(`You can call me at {{phone.number(+!# !## #### #####!)}}.') * ``` * * Currently it is not possible to set more than a single parameter. @@ -490,12 +490,12 @@ export class Helpers { * @see faker.helpers.mustache() to use custom functions for resolution. * * @example - * faker.fake('{{name.lastName}}') // 'Barrows' - * faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}') // 'Durgan, Noe MD' - * faker.fake('This is static test.') // 'This is static test.' - * faker.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!' - * faker.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.' - * faker.fake('I flipped the coin an got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails' + * faker.helpers.fake('{{name.lastName}}') // 'Barrows' + * faker.helpers.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}') // 'Durgan, Noe MD' + * faker.helpers.fake('This is static test.') // 'This is static test.' + * faker.helpers.fake('Good Morning {{name.firstName}}!') // 'Good Morning Estelle!' + * faker.helpers.fake('You can call me at {{phone.number(!## ### #####!)}}.') // 'You can call me at 202 555 973722.' + * faker.helpers.fake('I flipped the coin an got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails' */ fake(str: string): string { // if incoming str parameter is not provided, return error message From 54760ec2e195feafebcbef83516ab31dd3a7a3f2 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 23:10:08 +0200 Subject: [PATCH 05/11] refactor(fake): deprecate module --- src/modules/fake/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/modules/fake/index.ts b/src/modules/fake/index.ts index 9926f435987..a60acd5b05c 100644 --- a/src/modules/fake/index.ts +++ b/src/modules/fake/index.ts @@ -3,6 +3,8 @@ import { deprecated } from '../../internal/deprecated'; /** * Generator method for combining faker methods based on string input. + * + * @deprecated */ export class Fake { constructor(private readonly faker: Faker) { From 3a404f961e42608ee870c115ddcaa276a0cfe110 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 23:13:24 +0200 Subject: [PATCH 06/11] docs(readme): update faker.fake() reference --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 14192bc6abd..e55744cf9f3 100644 --- a/README.md +++ b/README.md @@ -98,11 +98,11 @@ The API covers the following modules: ### Templates -Faker contains a generator method `faker.fake` for combining faker API methods using a mustache string format. +Faker contains a generator method `faker.helpers.fake` for combining faker API methods using a mustache string format. ```ts console.log( - faker.fake('Hello {{name.prefix}} {{name.lastName}}, how are you today?') + faker.helpers.fake('Hello {{name.prefix}} {{name.lastName}}, how are you today?') ); ``` From a6b13ba9c8af82c08df1422768ca40b9b9b6e612 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 23:13:45 +0200 Subject: [PATCH 07/11] test(all_functional): update faker.fake() reference --- test/all_functional.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/all_functional.spec.ts b/test/all_functional.spec.ts index b391b61e86f..f857b84c6de 100644 --- a/test/all_functional.spec.ts +++ b/test/all_functional.spec.ts @@ -99,7 +99,7 @@ describe('functional tests', () => { } }); -describe('faker.fake functional tests', () => { +describe('faker.helpers.fake functional tests', () => { for (const locale in faker.locales) { describe(locale, () => { Object.keys(modules).forEach((module) => { @@ -109,7 +109,7 @@ describe('faker.fake functional tests', () => { faker.locale = locale; // TODO ST-DDT 2022-03-28: Use random seed once there are no more failures faker.seed(1); - const result = faker.fake(`{{${module}.${meth}}}`); + const result = faker.helpers.fake(`{{${module}.${meth}}}`); expect(result).toBeTypeOf('string'); }); From 07f8b64486e06854d919a7be2428069b9b9d7b38 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 18 Jul 2022 23:39:50 +0200 Subject: [PATCH 08/11] chore: formatting --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e55744cf9f3..e9c64f4da2c 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,9 @@ Faker contains a generator method `faker.helpers.fake` for combining faker API m ```ts console.log( - faker.helpers.fake('Hello {{name.prefix}} {{name.lastName}}, how are you today?') + faker.helpers.fake( + 'Hello {{name.prefix}} {{name.lastName}}, how are you today?' + ) ); ``` From db864ff34361eeb71dc7f053d67c7d972b479981 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 23 Jul 2022 16:49:22 +0200 Subject: [PATCH 09/11] test(helpers): fake with test factory --- test/__snapshots__/helpers.spec.ts.snap | 6 ++++++ test/helpers.spec.ts | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/test/__snapshots__/helpers.spec.ts.snap b/test/__snapshots__/helpers.spec.ts.snap index d959cafe430..1fb1428c2e9 100644 --- a/test/__snapshots__/helpers.spec.ts.snap +++ b/test/__snapshots__/helpers.spec.ts.snap @@ -29,6 +29,8 @@ exports[`helpers > 42 > arrayElements > with array 2`] = ` ] `; +exports[`helpers > 42 > fake > with plain string 1`] = `"my test string"`; + exports[`helpers > 42 > maybe > with only value 1`] = `"Hello World!"`; exports[`helpers > 42 > maybe > with value and probability 1`] = `undefined`; @@ -139,6 +141,8 @@ exports[`helpers > 1211 > arrayElements > with array 2`] = ` ] `; +exports[`helpers > 1211 > fake > with plain string 1`] = `"my test string"`; + exports[`helpers > 1211 > maybe > with only value 1`] = `undefined`; exports[`helpers > 1211 > maybe > with value and probability 1`] = `undefined`; @@ -239,6 +243,8 @@ exports[`helpers > 1337 > arrayElements > with array 2`] = ` ] `; +exports[`helpers > 1337 > fake > with plain string 1`] = `"my test string"`; + exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`; exports[`helpers > 1337 > maybe > with value and probability 1`] = `undefined`; diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index c15fe6c2bd5..84f817e987c 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -86,6 +86,10 @@ describe('helpers', () => { t.describe('objectValue', (t) => { t.it('simple', { a: 1, b: 2, c: 3 }); }); + + t.describe('fake', (t) => { + t.it('with plain string', 'my test string'); + }); }); describe(`random seeded tests for seed ${faker.seed()}`, () => { From 0af5f5f5a1ba1f80aebfb021fc3ed4e5cc62bf62 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Mon, 25 Jul 2022 22:47:00 +0200 Subject: [PATCH 10/11] test(helpers.fake): enhance test case --- test/__snapshots__/helpers.spec.ts.snap | 6 ++++++ test/helpers.spec.ts | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/test/__snapshots__/helpers.spec.ts.snap b/test/__snapshots__/helpers.spec.ts.snap index 1fb1428c2e9..c79f19c886c 100644 --- a/test/__snapshots__/helpers.spec.ts.snap +++ b/test/__snapshots__/helpers.spec.ts.snap @@ -29,6 +29,8 @@ exports[`helpers > 42 > arrayElements > with array 2`] = ` ] `; +exports[`helpers > 42 > fake > with args 1`] = `"my string: Cky2eiXX/J"`; + exports[`helpers > 42 > fake > with plain string 1`] = `"my test string"`; exports[`helpers > 42 > maybe > with only value 1`] = `"Hello World!"`; @@ -141,6 +143,8 @@ exports[`helpers > 1211 > arrayElements > with array 2`] = ` ] `; +exports[`helpers > 1211 > fake > with args 1`] = `"my string: wKti5-}$_/"`; + exports[`helpers > 1211 > fake > with plain string 1`] = `"my test string"`; exports[`helpers > 1211 > maybe > with only value 1`] = `undefined`; @@ -243,6 +247,8 @@ exports[`helpers > 1337 > arrayElements > with array 2`] = ` ] `; +exports[`helpers > 1337 > fake > with args 1`] = `"my string: 9U/4:SK$>6"`; + exports[`helpers > 1337 > fake > with plain string 1`] = `"my test string"`; exports[`helpers > 1337 > maybe > with only value 1`] = `"Hello World!"`; diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 84f817e987c..7c3368f4155 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -88,7 +88,10 @@ describe('helpers', () => { }); t.describe('fake', (t) => { - t.it('with plain string', 'my test string'); + t.it('with plain string', 'my test string').it( + 'with args', + 'my string: {{datatype.string}}' + ); }); }); From 5f0a70f4c1f16e0180d99e9ad33902da63f538b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leyla=20J=C3=A4hnig?= Date: Tue, 26 Jul 2022 19:09:21 +0200 Subject: [PATCH 11/11] test(helpers.fake): reword test description Co-authored-by: Eric Cheng --- test/helpers.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/helpers.spec.ts b/test/helpers.spec.ts index 7c3368f4155..61d742a7118 100644 --- a/test/helpers.spec.ts +++ b/test/helpers.spec.ts @@ -580,7 +580,7 @@ describe('helpers', () => { expect(faker.helpers.fake('}}hello')).toBe('}}hello'); }); - it('should be able to handle reverted brackets', () => { + it('should be able to handle inverted brackets', () => { expect(faker.helpers.fake('}}hello{{')).toBe('}}hello{{'); });