Sitelet https://github.com/nodejs/node/issues/51870
Skip to content

'node:test' - expose a seed to allow reproducible tests with pseudo random generators #51870

Description

@gerardolima

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

Activity

  1. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on Feb 25, 2024
  2. avivkeller commented on Apr 12, 2024

    @avivkeller
    Member

    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!

  3. gerardolima commented on Apr 14, 2024

    @gerardolima
    Author

    hey, @redyetidev, my suggestion is for node:test to provide a seed and support it on the command options, as Jest does with .getSeed().

  4. cjihrig commented on Apr 14, 2024

    @cjihrig
    Contributor

    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.

  5. avivkeller commented on Apr 14, 2024

    @avivkeller
    Member

    hey, @redyetidev, my suggestion is for node:test to 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!

  6. avivkeller commented on Apr 14, 2024

    @avivkeller
    Member

    Okay, I have an idea. I'm gonna do some fiddling on my fork of Node and figure something out.

  7. avivkeller commented on Apr 14, 2024

    @avivkeller
    Member

    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 :-).

  8. gerardolima commented on Apr 14, 2024

    @gerardolima
    Author

    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.

  9. avivkeller commented on Apr 15, 2024

    @avivkeller
    Member

    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) (where t is the TestContext)

  10. avivkeller commented on Apr 15, 2024

    @avivkeller
    Member

    After #52529 is resolved/merged, I will open a PR for this feature

  11. gerardolima commented on Apr 15, 2024

    @gerardolima
    Author

    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.

  12. self-assigned this
    on Apr 20, 2024
  13. avivkeller commented on Apr 20, 2024

    @avivkeller
    Member

    I've begun work on this, and I'll have a PR ready soon(ish).

  14. avivkeller commented on Apr 21, 2024

    @avivkeller
    Member

    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!

  15. gerardo-lima-moonfare commented on Jul 28, 2025

    @gerardo-lima-moonfare

    this would be a low hanging fruit that would improve adoption from other test runners/libraries, but it's alright, not everybody has to agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature requestIssues requesting new Node.js features.test_runnerIssues and PRs related to the test runner subsystem.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions