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

faker.helpers.maybe doesn't always invoke callback with { probability: 1 } #1713

Closed
8 of 10 tasks
NvGary opened this issue Jan 5, 2023 · 3 comments
Closed
8 of 10 tasks
Labels
c: bug Something isn't working m: helpers Something is referring to the helpers module

Comments

@NvGary
Copy link

NvGary commented Jan 5, 2023

Pre-Checks

Describe the bug

If you execute this block enough times you will experience an intermittent failure.

describe('maybe', () => {
    it('should always return the callback result when probability is 1', () => {
        const actual = faker.helpers.maybe(() => 'foo', { probability: 1 });

        expect(actual).toBe('foo');
    });
});

On dry reading the code this can happen when faker.datatype.float({ min: 0, max: 1}) returns 1. The subsequent 1 < 1 check fails and the callback is never executed.

Minimal reproduction code

describe('maybe', () => {
    it('should always return the callback result when probability is 1', () => {
        jest.spyOn(faker.datatype, 'float').mockReturnValueOnce(1);
        const actual = faker.helpers.maybe(() => 'foo', { probability: 1 });

        expect(actual).toBe('foo');
    });
});

Additional Context

A quick 1 <= 1 fix won't work as maybe should fail { probability: 0 } and faker.datatype.float returning 0.

Environment Info

System:
    OS: macOS 12.6.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 772.65 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Chrome: 108.0.5359.124
    Safari: 16.1

Which module system do you use?

  • CJS
  • ESM

Used Package Manager

npm

@NvGary NvGary added c: bug Something isn't working s: pending triage Pending Triage labels Jan 5, 2023
@ST-DDT
Copy link
Member

ST-DDT commented Jan 5, 2023

Which version are you testing on?

if (this.faker.datatype.boolean(options)) {
return callback();
}
return undefined;
}

if (probability >= 1) {
// This check is required to avoid returning false when float() returns 1
return true;
}

Should catch probability == 1 and return true.

@ST-DDT ST-DDT added the s: awaiting more info Additional information are requested label Jan 5, 2023
@ST-DDT
Copy link
Member

ST-DDT commented Jan 5, 2023

Yes in v7.6.0 that is a bug, but we fixed it since.

Fixed in #1476

Thanks for bringing this to our attention anyway.

@ST-DDT ST-DDT closed this as completed Jan 5, 2023
@ST-DDT ST-DDT added this to the v7 - Current Major milestone Jan 5, 2023
@ST-DDT ST-DDT added m: helpers Something is referring to the helpers module and removed s: pending triage Pending Triage s: awaiting more info Additional information are requested labels Jan 5, 2023
@NvGary
Copy link
Author

NvGary commented Jan 5, 2023

Schoolboy error. Was working on 7.6.0. Should have checked the latest version 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: bug Something isn't working m: helpers Something is referring to the helpers module
Projects
None yet
Development

No branches or pull requests

2 participants