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

feat: add class for mocking Math.random #233

Merged
merged 2 commits into from
Sep 23, 2024

Conversation

ojeytonwilliams
Copy link
Contributor

  • feat: add RandomMocker class to mock Math.random
  • docs: explain how RandomMocker can be used

Checklist:

This class lets us control Math.random in testing by replacing it with a pseudorandom number generator with a fixed seed.

@ilenia-magoni
Copy link

If this is made to be used in --before-user-code-- like in freeCodeCamp/freeCodeCamp#56151, there is a slight issue... helpers are not accessible in --before-user-code-- and --after-user-code--, there __helpers is not defined.

@ojeytonwilliams
Copy link
Contributor Author

That's a good point - when using this you have to instantiate the RandomMocker in the test code. For example, the linked issue would no be able to use --before-user-code-- and, instead, would do this in the test:

let mocker = new __helpers.RandomMocker();
mocker.mock();
try {
  rollDiceBtn.click();
  const old = [...diceValuesArr];
  rollDiceBtn.click();
  assert.lengthOf(diceValuesArr, 5);
  assert.notDeepEqual(old, diceValuesArr);
} finally {
  mocker.restore();
}

Copy link
Member

@naomi-lgbt naomi-lgbt left a comment

Choose a reason for hiding this comment

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

Don't mock Math.random(), it's trying its best and mocking it is mean 🙁

@ojeytonwilliams ojeytonwilliams merged commit 0318a15 into freeCodeCamp:main Sep 23, 2024
4 checks passed
@ojeytonwilliams ojeytonwilliams deleted the feat/random-mocker branch September 23, 2024 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants