Skip to content

Commit 110ea82

Browse files
authored
Move jest-dev-server to global (#121)
* Move jest-dev-server to global * Remove unused variable from PlaywrightEnvironment
1 parent bbcfe52 commit 110ea82

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

src/PlaywrightEnvironment.ts

+2-45
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,6 @@ const KEYS = {
2424
ENTER: '\r',
2525
}
2626

27-
let teardownServer: (() => Promise<void>) | null = null
28-
29-
const logMessage = ({
30-
message,
31-
action,
32-
}: {
33-
message: string
34-
action: string
35-
}): void => {
36-
console.log('')
37-
console.error(message)
38-
console.error(`\n☝️ You ${action} in jest-playwright.config.js`)
39-
process.exit(1)
40-
}
41-
4227
const getBrowserPerProcess = async (
4328
playwrightInstance: GenericBrowser,
4429
browserType: BrowserType,
@@ -77,7 +62,7 @@ export const getPlaywrightEnv = (basicEnv = 'node') => {
7762
const config = await readConfig(this._config.rootDir)
7863
//@ts-ignore
7964
const browserType = getBrowserType(this._config.browserName)
80-
const { context, exitOnPageError, server, selectors } = config
65+
const { context, exitOnPageError, selectors } = config
8166
const playwrightPackage = await readPackage()
8267
if (playwrightPackage === IMPORT_KIND_PLAYWRIGHT) {
8368
// eslint-disable-next-line @typescript-eslint/no-var-requires
@@ -98,30 +83,6 @@ export const getPlaywrightEnv = (basicEnv = 'node') => {
9883
)
9984
let contextOptions = context
10085

101-
if (server) {
102-
// eslint-disable-next-line @typescript-eslint/no-var-requires
103-
const devServer = require('jest-dev-server')
104-
const { setup, ERROR_TIMEOUT, ERROR_NO_COMMAND } = devServer
105-
teardownServer = devServer.teardown
106-
try {
107-
await setup(server)
108-
} catch (error) {
109-
if (error.code === ERROR_TIMEOUT) {
110-
logMessage({
111-
message: error.message,
112-
action: 'can set "server.launchTimeout"',
113-
})
114-
}
115-
if (error.code === ERROR_NO_COMMAND) {
116-
logMessage({
117-
message: error.message,
118-
action: 'must set "server.command"',
119-
})
120-
}
121-
throw error
122-
}
123-
}
124-
12586
if (device) {
12687
const { viewport, userAgent } = playwright.devices[device]
12788
contextOptions = { viewport, userAgent, ...contextOptions }
@@ -192,7 +153,7 @@ export const getPlaywrightEnv = (basicEnv = 'node') => {
192153
}
193154
}
194155

195-
async teardown(jestConfig: JestConfig.InitialOptions = {}): Promise<void> {
156+
async teardown(): Promise<void> {
196157
const { page, browser } = this.global
197158
if (page) {
198159
page.removeListener('pageerror', handleError)
@@ -203,10 +164,6 @@ export const getPlaywrightEnv = (basicEnv = 'node') => {
203164
}
204165

205166
await super.teardown()
206-
207-
if (!jestConfig.watch && !jestConfig.watchAll && teardownServer) {
208-
await teardownServer()
209-
}
210167
}
211168
}
212169
}

src/global.ts

+59-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,60 @@
1-
// TODO Check this global methods
2-
// eslint-disable-next-line @typescript-eslint/no-empty-function
3-
export const setup = (): void => {}
1+
/* eslint-disable no-console */
2+
import {
3+
setup as setupServer,
4+
teardown as teardownServer,
5+
ERROR_TIMEOUT,
6+
ERROR_NO_COMMAND,
7+
} from 'jest-dev-server'
8+
import { readConfig } from './utils'
9+
import type { Config as JestConfig } from '@jest/types'
410

5-
// eslint-disable-next-line @typescript-eslint/no-empty-function
6-
export const teardown = (): void => {}
11+
let didAlreadyRunInWatchMode = false
12+
13+
const logMessage = ({
14+
message,
15+
action,
16+
}: {
17+
message: string
18+
action: string
19+
}): void => {
20+
console.log('')
21+
console.error(message)
22+
console.error(`\n☝️ You ${action} in jest-playwright.config.js`)
23+
process.exit(1)
24+
}
25+
26+
export async function setup(jestConfig: JestConfig.GlobalConfig) {
27+
const config = await readConfig(jestConfig.rootDir)
28+
29+
// If we are in watch mode, - only setupServer() once.
30+
if (jestConfig.watch || jestConfig.watchAll) {
31+
if (didAlreadyRunInWatchMode) return
32+
didAlreadyRunInWatchMode = true
33+
}
34+
35+
if (config.server) {
36+
try {
37+
await setupServer(config.server)
38+
} catch (error) {
39+
if (error.code === ERROR_TIMEOUT) {
40+
logMessage({
41+
message: error.message,
42+
action: 'can set "server.launchTimeout"',
43+
})
44+
}
45+
if (error.code === ERROR_NO_COMMAND) {
46+
logMessage({
47+
message: error.message,
48+
action: 'must set "server.command"',
49+
})
50+
}
51+
throw error
52+
}
53+
}
54+
}
55+
56+
export async function teardown(jestConfig: JestConfig.GlobalConfig) {
57+
if (!jestConfig.watch && !jestConfig.watchAll) {
58+
await teardownServer()
59+
}
60+
}

0 commit comments

Comments
 (0)