Using with Playwright
With the Global Setup/Teardown and Async Test Environment APIs, Jest can work smoothly with Playwright.
#
Use jest-playwright Presetjest-playwright-preset provides all required configuration to run your tests using Playwright.
- First, install
jest-playwright-preset
andplaywright
- Specify preset in your Jest configuration:
- Write your test
There's no need to launch the browser manually. Playwright's page
, context
, and browser
classes will automatically be exposed as global variables see here.
You can also create a new context and page for each test to have them isolated from each other, see here.
#
Custom example without jest-playwright-presetYou can also hook up Playwright with Jest from scratch. The basic idea is to:
- launch a Playwright BrowserServer and save the websocket endpoint to disk with Global Setup
- connect to Playwright from each Test Environment
- close the BrowserServer with Global Teardown
We have a small constants file where we store our shared used variables:
Here's an example of the GlobalSetup script
Then we need a custom Test Environment for Playwright
Finally, we can close the Playwright BrowserServer and clean-up the file
With all the things set up, we can now write our tests like this:
Finally, set jest.config.js
to read from these files. (The jest-playwright-preset
does something like this under the hood.)
Here's the code of a full working example and here with ES modules.