-
Notifications
You must be signed in to change notification settings - Fork 289
/
global.js
72 lines (64 loc) · 1.76 KB
/
global.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable no-console */
import {
setup as setupServer,
teardown as teardownServer,
ERROR_TIMEOUT,
ERROR_NO_COMMAND,
} from 'jest-dev-server'
import chalk from 'chalk'
import { readConfig, getPuppeteer } from './readConfig'
let browser
let didAlreadyRunInWatchMode = false
export async function setup(jestConfig = {}) {
const config = await readConfig()
const puppeteer = getPuppeteer(config)
if (config.connect) {
browser = await puppeteer.connect(config.connect)
} else {
browser = await puppeteer.launch(config.launch)
}
process.env.PUPPETEER_WS_ENDPOINT = browser.wsEndpoint()
// If we are in watch mode, - only setupServer() once.
if (jestConfig.watch || jestConfig.watchAll) {
if (didAlreadyRunInWatchMode) return
didAlreadyRunInWatchMode = true
}
if (config.server) {
try {
await setupServer(config.server)
} catch (error) {
if (error.code === ERROR_TIMEOUT) {
console.log('')
console.error(chalk.red(error.message))
console.error(
chalk.blue(
`\n☝️ You can set "server.launchTimeout" in jest-puppeteer.config.js`,
),
)
process.exit(1)
}
if (error.code === ERROR_NO_COMMAND) {
console.log('')
console.error(chalk.red(error.message))
console.error(
chalk.blue(
`\n☝️ You must set "server.command" in jest-puppeteer.config.js`,
),
)
process.exit(1)
}
throw error
}
}
}
export async function teardown(jestConfig = {}) {
const config = await readConfig()
if (config.connect) {
await browser.disconnect()
} else {
await browser.close()
}
if (!jestConfig.watch && !jestConfig.watchAll) {
await teardownServer()
}
}