Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(helpers): deprecate invocations on non-functions for fakeEval #2913

Merged
merged 10 commits into from
Jun 14, 2024
16 changes: 16 additions & 0 deletions docs/guide/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,19 @@ If you wanted more control over the number, it was previously necessary to pass
The styles are locale-aware, so for example if you use pt_PT, phone numbers suitable for Portugal would be generated.

2. If none of the `style`s match your needs, you can use `faker.string.numeric()` or `faker.helpers.fromRegExp()` to create a custom pattern.

### Fake now Fails for Invoking Functions on Non-Functions

Previously, function calls on non-functions were silently ignored.

```ts
faker.helpers.fake('{{person.first_name()}}''); // 'Jenny'
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
```

Now, it fails with an exception:

```txt
FakerError: Cannot resolve expression 'person.first_name()'
```

This makes the fake method more consistent with standard JavaScript behavior.
4 changes: 1 addition & 3 deletions src/modules/helpers/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ function evalProcessFunction(
return [
index + (nextChar === '.' ? 2 : 1), // one for the closing bracket, one for the dot
entrypoints.map((entrypoint): unknown =>
// TODO @ST-DDT 2023-12-11: Replace in v9
// typeof entrypoint === 'function' ? entrypoint(...params) : undefined
typeof entrypoint === 'function' ? entrypoint(...params) : entrypoint
typeof entrypoint === 'function' ? entrypoint(...params) : undefined
),
];
}
Expand Down
11 changes: 4 additions & 7 deletions test/modules/helpers-eval.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,10 @@ describe('fakeEval()', () => {
});

it('requires a function for parameters', () => {
// TODO @ST-DDT 2023-12-11: Replace in v9
// expect(faker.definitions.person.first_name).toBeDefined();
//expect(() => fakeEval('person.first_name()', faker)).toThrow(
// new FakerError(`Cannot resolve expression 'person.first_name'`)
// );
const actual = fakeEval('person.first_name()', faker);
expect(faker.definitions.person.first_name).toContain(actual);
expect(faker.definitions.person.first_name).toBeDefined();
expect(() => fakeEval('person.first_name()', faker)).toThrow(
new FakerError(`Cannot resolve expression 'person.first_name()'`)
);
});

it('requires a valid expression (missing value)', () => {
Expand Down