@@ -9,7 +9,7 @@ import type {
99 TestRunnerOptions ,
1010} from 'jest-runner'
1111import type { Config as JestConfig } from '@jest/types'
12- import type { BrowserType , GenericBrowser } from './types'
12+ import type { BrowserType } from './types'
1313import {
1414 checkBrowserEnv ,
1515 checkDeviceEnv ,
@@ -53,40 +53,8 @@ const getBrowserTest = (
5353 }
5454}
5555
56- const getTests = async ( tests : Test [ ] ) : Promise < Test [ ] > => {
57- const playwrightPackage = await readPackage ( )
58- const pwTests : Test [ ] = [ ]
59- const browser2Server : Partial < Record < BrowserType , BrowserServer > > = { }
60- for ( const test of tests ) {
61- const { rootDir } = test . context . config
62- const { browsers, devices, launchBrowserApp } = await readConfig ( rootDir )
63- for ( const browser of browsers ) {
64- checkBrowserEnv ( browser )
65- const { devices : availableDevices , instance } = getPlaywrightInstance (
66- playwrightPackage ,
67- browser ,
68- )
69- if ( ! browser2Server [ browser ] ) {
70- browser2Server [ browser ] = await instance . launchServer ( launchBrowserApp )
71- }
72- const wsEndpoint = browser2Server [ browser ] ! . wsEndpoint ( )
73-
74- if ( devices && devices . length ) {
75- devices . forEach ( ( device ) => {
76- const availableDeviceNames = Object . keys ( availableDevices )
77- checkDeviceEnv ( device , availableDeviceNames )
78- pwTests . push ( getBrowserTest ( test , browser , wsEndpoint , device ) )
79- } )
80- } else {
81- pwTests . push ( getBrowserTest ( test , browser , wsEndpoint , null ) )
82- }
83- }
84- }
85-
86- return pwTests
87- }
88-
8956class PlaywrightRunner extends JestRunner {
57+ browser2Server : Partial < Record < BrowserType , BrowserServer > >
9058 constructor (
9159 globalConfig : JestConfig . GlobalConfig ,
9260 context : TestRunnerContext ,
@@ -95,6 +63,41 @@ class PlaywrightRunner extends JestRunner {
9563 // Set default timeout to 15s
9664 config . testTimeout = config . testTimeout || DEFAULT_TEST_PLAYWRIGHT_TIMEOUT
9765 super ( config , context )
66+ this . browser2Server = { }
67+ }
68+
69+ async getTests ( tests : Test [ ] ) : Promise < Test [ ] > {
70+ const playwrightPackage = await readPackage ( )
71+ const pwTests : Test [ ] = [ ]
72+ for ( const test of tests ) {
73+ const { rootDir } = test . context . config
74+ const { browsers, devices, launchBrowserApp } = await readConfig ( rootDir )
75+ for ( const browser of browsers ) {
76+ checkBrowserEnv ( browser )
77+ const { devices : availableDevices , instance } = getPlaywrightInstance (
78+ playwrightPackage ,
79+ browser ,
80+ )
81+ if ( ! this . browser2Server [ browser ] ) {
82+ this . browser2Server [ browser ] = await instance . launchServer (
83+ launchBrowserApp ,
84+ )
85+ }
86+ const wsEndpoint = this . browser2Server [ browser ] ! . wsEndpoint ( )
87+
88+ if ( devices && devices . length ) {
89+ devices . forEach ( ( device ) => {
90+ const availableDeviceNames = Object . keys ( availableDevices )
91+ checkDeviceEnv ( device , availableDeviceNames )
92+ pwTests . push ( getBrowserTest ( test , browser , wsEndpoint , device ) )
93+ } )
94+ } else {
95+ pwTests . push ( getBrowserTest ( test , browser , wsEndpoint , null ) )
96+ }
97+ }
98+ }
99+
100+ return pwTests
98101 }
99102
100103 async runTests (
@@ -105,9 +108,9 @@ class PlaywrightRunner extends JestRunner {
105108 onFailure : OnTestFailure ,
106109 options : TestRunnerOptions ,
107110 ) : Promise < void > {
108- const browserTests = await getTests ( tests )
111+ const browserTests = await this . getTests ( tests )
109112
110- return await ( options . serial
113+ await ( options . serial
111114 ? this [ '_createInBandTestRun' ] (
112115 browserTests ,
113116 watcher ,
@@ -122,6 +125,11 @@ class PlaywrightRunner extends JestRunner {
122125 onResult ,
123126 onFailure ,
124127 ) )
128+
129+ for ( const browser in this . browser2Server ) {
130+ await this . browser2Server [ browser as BrowserType ] ! . close ( )
131+ }
132+ return
125133 }
126134}
127135
0 commit comments