question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

question regarding globalSetup and globalTeardown

See original GitHub issue

I am attempting to utilize globalSetup and globalTeardown to implement authentication once per suite rather than once per test.

In jest.config I customize the browsers array via environment variables like so

let browsers = ['chromium'];

if (process.env.E2E_BROWSERS) {
  browsers = process.env.E2E_BROWSERS.split(' ');
}

What is the proper way to inject the storage state into the currently available browser? The examples in the documentation assume the use of just one browser, chromium.

I saw an example from @mmarkelov that might be relevant but it appears to have been written a few months before PlaywrightEnvironment was introduced to jest-playwright

Any guidance is greatly appreciated!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cutterblcommented, May 6, 2021

The globalSetup example looks like this

// global-setup.js
import { globalSetup as playwrightGlobalSetup } from 'jest-playwright-preset';

module.exports = async function globalSetup(globalConfig) {
  await playwrightGlobalSetup(globalConfig);

  const browserServer = await chromium.launchServer();
  const wsEndpoint = browserServer.wsEndpoint();
  const browser = await chromium.connect({ wsEndpoint: wsEndpoint });
  const page = await browser.newPage();

  // your login function
  await doLogin(page);

  // store authentication data
  const storage = await page.context().storageState();
  process.env.STORAGE = JSON.stringify(storage);
};

So, in the example you have to specify the browser being used, rather than the one utilized from the browsers array in the jest-playwright.config.js. So the question becomes, how would you use the current browser for your login process.

Seems to me that the globalSetup runs prior to all of the tests being run, so globalSetup probably doesn’t know which browser your tests are going to run against, and probably wouldn’t run again if you’ve defined multiple browsers, but I don’t know enough about jest internals, so I’m just assuming. If that’s the case, I guess the developer just has to pick one (browser) to write the login against (to get the state data). If someone could verify that, that would be great.

0reactions
cutterblcommented, Jul 23, 2021

I wrote a blog post about how I approached persistent login using globalSetup. Spent a lot of time going over the documentation in my work to figure this out. Maybe it’ll help someone else stumbling on this thread. If there are ways to improve it, I am always open to suggestions.

Playwright and Jest-Playwright

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make "globalSetup" and "globalTeardown" work together with ...
Modules defined in globalSetup and globalTeardown are not being transformed as defined in transform configuration entry.
Read more >
Getting Jest global setup and global teardown to work in a ...
The typescript environment is not yet defined when globalSetup and globalTeardown are run, which is why jest is failing.
Read more >
Configuring Jest
Any global variables that are defined through globalSetup can only be read in globalTeardown . You cannot retrieve globals defined here in ...
Read more >
Internals API | Detox
Theoretically, Detox CLI could be totally agnostic about the test runner under the hood, ... Hence, neither globalSetup nor globalTeardown will be called, ......
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
Do not share a context with tests, suites, or other hooks. There are two types of global fixtures: global setup fixtures and global...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found