Repository navigation
'node:test' - expose a seed to allow reproducible tests with pseudo random generators #51870
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Feb 25, 2024 - addedtest_runnerIssues and PRs related to the test runner subsystem.Issues and PRs related to the test runner subsystem.
on Feb 25, 2024 Hi, @gerardolima!
When you refer to 'pseudo-random generators,' what exactly did you consider?
Math.random()itself isn't seeded, as ECMAScript doesn't tell to be:21.3.2.27 Math.random ( )
This function returns a Number value with positive sign, greater than or equal to +0𝔽 but strictly less than 1𝔽, chosen randomly or pseudo randomly with approximately uniform distribution over that range, using an implementation-defined algorithm or strategy.Each Math.random function created for distinct realms must produce a distinct sequence of values from successive calls.
For other pseudo-random generators, seeding would be up to the generator.
If I'm misunderstanding your request, please let me know!
hey, @redyetidev, my suggestion is for
node:testto provide a seed and support it on the command options, as Jest does with .getSeed().Node's test runner does not currently support any type of randomness, so I don't believe it currently makes sense for this feature to be added.
hey, @redyetidev, my suggestion is for
node:testto provide a seed and support it on the command options, as Jest does with .getSeed().Oh, I understand. You want the test runner to expose a seed to be used in a userland pseudorandom generator.
While @cjihrig is correct that Node's test runner does not currently use any randomness, it might not hurt to let the users have a seed.Theoretically, we could use some kind of
Math.random()calculation to give the user a number, but that wouldn't be a 'seed'. It's a plausible suggestion, but I'm not sure of the best way to implement it. I'll do some research into it.Thanks for the suggestion!
Reacted by Gerardo LimaOkay, I have an idea. I'm gonna do some fiddling on my fork of Node and figure something out.
Okay, @gerardolima. In my fork, https://github.com/redyetidev/node/tree/test-random, I have implemented this functionality. I currently have a separate PR editing the same file, and I'd prefer not to cause conflicts with it, but once it's resolved, I will open a PR for this feature. Until then, feel free to review my implementation and give suggestions :-).
The changes on your branch look good to me. I would suggest you to, please, also add a flag to control whether to show the seed value when running
node test.The approach I have for tests is to run them on the pipeline with random seeds and, when some test fail, I repeat using the specific seed I get from the logs. If this value is not output it would be still impossible to use this flow.
I would suggest you to, please, also add a flag to control whether to show the seed value when running
node test.In order for the flag to be shown, you'd have to run something along the lines of
console.log(t.seed)(wheretis theTestContext)Reacted by Gerardo LimaAfter #52529 is resolved/merged, I will open a PR for this feature
As the seed should be supported by the Node cli, it would make sense to me that this information could be output by the cli as well.
I've begun work on this, and I'll have a PR ready soon(ish).
Unfortunately, it doesn’t look like this feature will be added to NodeJS.
an alternative would be to use environment variables to set a fixed seed.
Thanks for your suggestion!
Reacted by Gerardo Limathis would be a low hanging fruit that would improve adoption from other test runners/libraries, but it's alright, not everybody has to agree
What is the problem this feature will solve?
I use a pseudo random generator on my unit tests (ie). For the tests to be reproducible, I am seeding the random generator with an ENV variable (
process.env['RANDOM_SEED']).What is the feature you are proposing to solve the problem?
I believe it would be clearer if the TestContext would provide a documented seed, as Jest provides with Jest.getSeed().
What alternatives have you considered?
No response