1 |
|
2 | const NodeEnvironment = require('jest-environment-node')
|
3 | const puppeteer = require('puppeteer')
|
4 | const fs = require('fs')
|
5 | const os = require('os')
|
6 | const path = require('path')
|
7 |
|
8 | const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup')
|
9 |
|
10 | class PuppeteerEnvironment extends NodeEnvironment {
|
11 | async setup() {
|
12 | console.log('Setup Test Environment.')
|
13 | await super.setup()
|
14 | const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8')
|
15 | if (!wsEndpoint) {
|
16 | throw new Error('wsEndpoint not found')
|
17 | }
|
18 | this.global.__BROWSER__ = await puppeteer.connect({ browserWSEndpoint: wsEndpoint })
|
19 | }
|
20 |
|
21 | async teardown() {
|
22 | console.log('Teardown Test Environment.')
|
23 | await super.teardown()
|
24 | }
|
25 |
|
26 | runScript(script) {
|
27 | return super.runScript(script)
|
28 | }
|
29 | }
|
30 |
|
31 | module.exports = PuppeteerEnvironment
|
32 |
|