Skip to content

feat(helpers): allow calling functions in fake placeholders#3841

Open
hiSandog wants to merge 2 commits intofaker-js:nextfrom
hiSandog:fix/helpers-fake-instance-members-20260503
Open

feat(helpers): allow calling functions in fake placeholders#3841
hiSandog wants to merge 2 commits intofaker-js:nextfrom
hiSandog:fix/helpers-fake-instance-members-20260503

Conversation

@hiSandog
Copy link
Copy Markdown
Contributor

@hiSandog hiSandog commented May 3, 2026

Summary

  • bind methods resolved from objects and primitive values in helpers.fake evaluation
  • allow chained instance property/method access after faker function references and calls
  • add regression coverage for primitive length, string method, and Date method chains

Fixes #3465.

Validation

  • pnpm vitest run test/modules/helpers.spec.ts --testNamePattern "fake"
  • pnpm exec tsc --noEmit
  • pnpm exec prettier --check src/modules/helpers/eval.ts test/modules/helpers.spec.ts
  • pnpm exec eslint src/modules/helpers/eval.ts test/modules/helpers.spec.ts
  • git diff --check

@hiSandog hiSandog requested a review from a team as a code owner May 3, 2026 02:25
@netlify
Copy link
Copy Markdown

netlify Bot commented May 3, 2026

Deploy Preview for fakerjs ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit a40cb4a
🔍 Latest deploy log https://app.netlify.com/projects/fakerjs/deploys/69f8cc8d7a13be0008a6bd34
😎 Deploy Preview https://deploy-preview-3841.fakerjs.dev
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 3, 2026

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.87%. Comparing base (f433771) to head (a40cb4a).
⚠️ Report is 11 commits behind head on next.

Files with missing lines Patch % Lines
src/modules/helpers/eval.ts 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             next    #3841      +/-   ##
==========================================
- Coverage   98.90%   98.87%   -0.03%     
==========================================
  Files         899      900       +1     
  Lines        3113     3122       +9     
  Branches      574      578       +4     
==========================================
+ Hits         3079     3087       +8     
- Misses         30       31       +1     
  Partials        4        4              
Files with missing lines Coverage Δ
src/modules/helpers/index.ts 95.27% <ø> (ø)
src/modules/helpers/eval.ts 94.80% <90.00%> (-0.85%) ⬇️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ST-DDT ST-DDT added c: feature Request for new feature p: 1-normal Nothing urgent c: security Indicates a vulnerability m: helpers Something is referring to the helpers module labels May 3, 2026
@ST-DDT ST-DDT added this to the v10.x milestone May 3, 2026
@ST-DDT
Copy link
Copy Markdown
Member

ST-DDT commented May 3, 2026

Can you please restrict access to constructor, prototype and similar special properties?

@ST-DDT
Copy link
Copy Markdown
Member

ST-DDT commented May 3, 2026

And don't forget to adjust the JSDocs +examples on faker.helpers.fake.

@hiSandog
Copy link
Copy Markdown
Contributor Author

hiSandog commented May 4, 2026

Thanks for the review. I pushed a follow-up in a40cb4a that blocks constructor, prototype, and __proto__ from fake expression resolution, with coverage in both fakeEval and faker.helpers.fake tests. I also updated the faker.helpers.fake JSDoc examples to cover chained property resolution.

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.

Please also remove/update this hint.

  • It is also NOT possible to use any non-faker methods or plain javascript in such patterns.

* faker.helpers.fake('You can visit me at {{location.streetAddress(true)}}.') // 'You can visit me at 3393 Ronny Way Apt. 742.'
* faker.helpers.fake('I flipped the coin and got: {{helpers.arrayElement(["heads", "tails"])}}') // 'I flipped the coin and got: tails'
* faker.helpers.fake('Your PIN number is: {{string.numeric(4, {"exclude": ["0"]})}}') // 'Your PIN number is: 4834'
* faker.helpers.fake('Generated code has length {{string.alphanumeric(10).length}}.') // 'Generated code has length 10.'
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.

Please also add an example with a function call. e.g. date.toISOString()

*/
function resolveProperty(entrypoint: unknown, key: string): unknown {
if (BLOCKED_PROPERTIES.has(key)) {
return undefined;
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.

IMO this should throw. Because these should never be accessible.

@ST-DDT ST-DDT changed the title fix: bind fake helper instance methods feat(helpers): allow calling functions in fake placeholders May 4, 2026
@ST-DDT
Copy link
Copy Markdown
Member

ST-DDT commented May 4, 2026

@faker-js/maintainers Any suggestion for improving the PR title to better express the change?

@Shinigami92
Copy link
Copy Markdown
Member

@faker-js/maintainers Any suggestion for improving the PR title to better express the change?

Think it is fine now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c: feature Request for new feature c: security Indicates a vulnerability m: helpers Something is referring to the helpers module p: 1-normal Nothing urgent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

helpers.fake - patterns after root expression fail when referencing instance members

3 participants