← Back to Discord Forum

Global variables in parallelism

I am wondering if I have global variable and some utility function (ex: call an endpoint to create user). I call the endpoint and store the userid in the global variable so I can later use it in a different function. What would happen if I call the function (the endpoint to create user) in the beforeEach hook and run the tests in parallel? Each test will run the function and pass the data to the global variable. Does each test going to use the variable as its own? or it's going to create bad data? How to deal with this kinds of cases?

This thread is trying to answer question "What would happen if I call a function that uses a global variable in the beforeEach hook and run the tests in parallel? How to deal with this kind of case?"

2 replies

Hi, for parallel running of tests, Playwright uses isolated worker processes, so parallel tests won't influence each other's global variables.

However, global variables are not reset for each test executed within the same worker. So that's something to be aware of.

I am trying to avoid global variables. But in the NodeJS world they are less of an issue since everything is single-threaded. One way of avoiding global variables is having functions return a value, so a test can store that in a local variable.

it is possible to share state between different processes by using custom reporter, see example here https://github.com/cenfun/monocart-reporter#global-state-management

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.