@zyf2e/mitojs
Version:
A SDK for monitoring browser errors
44 lines (36 loc) • 1.16 kB
JavaScript
/* eslint-disable @typescript-eslint/no-var-requires */
const chalk = require('chalk')
const NodeEnvironment = require('jest-environment-node')
const puppeteer = require('puppeteer')
const fs = require('fs')
const os = require('os')
const path = require('path')
const DIR = path.join(os.tmpdir(), 'jest_puppeteer_global_setup')
class PuppeteerEnvironment extends NodeEnvironment {
constructor(config) {
super(config)
}
async setup() {
// console.log(chalk.yellow('Setup Test Environment.'))
await super.setup()
const wsEndpoint = fs.readFileSync(path.join(DIR, 'wsEndpoint'), 'utf8')
if (!wsEndpoint) {
throw new Error('wsEndpoint not found')
}
this.global.__BROWSER__ = await puppeteer.connect({
browserWSEndpoint: wsEndpoint
})
}
async teardown() {
console.log(chalk.yellow('Teardown Test Environment.'))
process.on('unhandledRejection', (reason) => {
// console.error('Unhandled Rejection at:', 'reason:', reason)
// process.exit(1)
})
await super.teardown()
}
runScript(script) {
return super.runScript(script)
}
}
module.exports = PuppeteerEnvironment