Repository navigation
feat(experimental): add vi.useFakeRandom and restoreRandom - #11933
sheremet-va wants to merge 1 commit into
Conversation
✅ Deploy Preview for vitest-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
vi.useFakeRandom and restoreRandomvi.useFakeRandom and restoreRandom
7d0520d to
4198fdc
Compare
d249277 to
a362950
Compare
|
|
||
| - Values from libraries that keep random bytes in a pool, such as `nanoid` and `uuid`. They use the bytes of earlier calls first, so the values of a test depend on the tests before it in the same file. | ||
| - Keys, signatures and other values that OpenSSL creates in `node:crypto` and `crypto.subtle`. Load keys from fixed data in a fixture instead. | ||
| - Values in workers, child processes and iframes. They have their own random functions. |
There was a problem hiding this comment.
This probably doesn't need to be called out. We don't mention it in mocking guide either.
| - Values in workers, child processes and iframes. They have their own random functions. |
| // `vi.useFakeRandom()` replaces `Math.random` | ||
| const random = Math.random |
There was a problem hiding this comment.
Do you think it would it make sense to move packages/spy/src/primordials.ts to the utils package and import from it as needed?
e14a0da to
f1c7c11
Compare
a29810a to
e53cb78
Compare
e53cb78 to
fc2aacf
Compare
fc2aacf to
bf53f3c
Compare
| - [`vi.createRandom`](/api/vi#vi-createrandom) in code without a test context, such as suite hooks, fixtures of file or worker scope, and setup files. | ||
| - [`project.createRandom`](/api/advanced/test-project#createrandom) in a [global setup file](/config/globalsetup). | ||
|
|
||
| For code that calls `Math.random()` or `crypto.randomUUID()`, use [`vi.useFakeRandom`](/api/vi#vi-usefakerandom). |
There was a problem hiding this comment.
Nit, the doc narrative reads abrupt and terse. Some wording suggestions:
For code that calls builtin random APIs such as
Math.random()orcrypto.randomUUID(), usevi.useFakeRandomso that the functions returns random values based on same seed option.
There was a problem hiding this comment.
I wouldn't review docs yet, they are too AI slopped (in any PR). I gave up on them because with every change it would add new statements to docs and make it sound worse. I want to go over them after everything is done
|
|
||
| - **Type:** `boolean` | ||
|
|
||
| `true` runs the test with the fake functions of [`vi.useFakeRandom()`](/api/vi#vi-usefakerandom), so `Math.random` and the random functions of `crypto` return values from the [seed](/config/seed) of the test. After the test, Vitest restores the functions that the test started with: |
There was a problem hiding this comment.
I realized this API doesn't seem to cover the code inside aroundEach since aroundEach itself wraps onBeforeTryTask. Probably it's not actionable but mentioning it since the doc's phrasing true runs the test with the fake functions leaving this vague.
There was a problem hiding this comment.
It should cover aroundEach, I think. If it doesn't it's a bug
There was a problem hiding this comment.
Right, this doesn't look intended and this has pre-existed for a long time (also affects like traceTimeline and mock reset too). This can be probably addressed separately from random PR.
There was a problem hiding this comment.
I'm gonna fix the seed use case only in this PR by not using onBeforeTryTask
bf53f3c to
e5c70df
Compare
Stacked on #11930. Contains the commit of #11956 until it merges.
Adds experimental
vi.useFakeRandom(),vi.useRealRandom()andvi.isFakeRandom(). They replaceMath.random,crypto.getRandomValues,crypto.randomUUIDand, in Node.js,randomBytes,randomFill,randomFillSync,randomIntandrandomUUIDofnode:crypto(also named imports) with functions that return values from theseedoption.context.random.fork('vitest:globals'), so a test keeps its values when tests are filtered or shuffled, and each retry starts again.beforeAll,afterAllandaroundAlluse a seed of the suite, fixtures of file or worker scope use a seed of the fixture, and other code uses a seed of the file.sequence.hooks: 'parallel').fakeRandom: { enableGlobally: true }callsvi.useFakeRandom()before the setup files and the test file load, and again before each test if a test restored the functions. Libraries that keep a reference when they load need it (lodash keepsMath.random,uuidkeepsrandomUUID).restoreRandom: truecallsvi.useRealRandom()before each test, likerestoreMocks. It cannot be used together withfakeRandom.enableGlobally.seedin the config.Not covered (documented): libraries that keep random bytes in a pool (
nanoid,uuid), values that OpenSSL creates (key generation, some signatures), and other realms (workers, child processes, iframes).