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

How can I set a global variable (i.e an access token) via globalSetup? #7601

Closed
khaledosman opened this issue Jan 10, 2019 · 4 comments
Closed

Comments

@khaledosman
Copy link

I know this has been asked before but I wasn't able to find an answer..
in my case I need to login to some API and get an access token that all the other tests can use / rely on to make the subsequent calls.

I setup a globalSetup option in my package.json where the function is being called successfully and calling the API... however I can't seem to set the returned access token to global and use it from other files, it just fails silently.

Reading through other answers I saw references to using setupFiles or beforeAll, but that doesn't help my case as they're run before each test or before each file respectively, I want to be able to run it once per setup and use it across multiple files and tests.

@thymikee
Copy link
Collaborator

You can store it in your file system so that it's available for all tests. You can also run a pre test script that will populate and env variable and use that inside. There is a bunch of possibilities out there but Jest doesn't have a dedicated API to make this use case easier.

Closing as this is not a help forum.

@eddiemonge
Copy link
Contributor

globalSetup seems exactly like the place this would be done. The fact that it can't seems like a limitation.

@SimenB
Copy link
Member

SimenB commented Feb 7, 2019

globalSetup runs outside of the context tests run in - otherwise they wouldn't be sandboxed.

However, we have discussed having globalSetup return som serializable value that we could inject into tests: #7184

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants