Skip to content

Instantly share code, notes, and snippets.

@myobie
Created April 20, 2020 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/1b2068317e07b4bfd0087ae937382f61 to your computer and use it in GitHub Desktop.
Save myobie/1b2068317e07b4bfd0087ae937382f61 to your computer and use it in GitHub Desktop.
const handler = require('serve-handler')
const { createServer } = require('http')
const launcher = require('chrome-launcher')
const remoteInterface = require('chrome-remote-interface')
let exitStatus = 0
const baseURL = 'http://localhost:6000'
const server = createServer((request, response) => {
return handler(request, response)
})
server.listen(6000, async () => {
console.log(`Running at ${baseURL}`)
const chrome = await launcher.launch({
startingUrl: `${baseURL}/blank`,
chromeFlags: ['--headless', '--disable-gpu', '--no-sandbox', '--explicitly-allowed-ports=6000'],
logLevel: 'info'
})
const tid = setTimeout(() => {
console.log('timed out')
chrome.kill().then(() => {
process.exit(1)
})
}, 50000)
console.log(`Chrome debugging port running on ${chrome.port}`)
let client
try {
client = await remoteInterface({ port: chrome.port })
const { Page, Runtime } = client
await Promise.all([
Runtime.enable(),
Page.enable()
])
const finishedLogs = new Promise((resolve, reject) => {
Runtime.consoleAPICalled((entry) => {
clearTimeout(tid)
if (entry.args.length === 1 &&
entry.args[0].type === 'string') {
const value = entry.args[0].value.trim()
console.log(value)
if (value.startsWith('# fail ')) {
const failed = value.replace(/^# fail /, '')
if (failed === '0') {
resolve(true)
} else {
reject(new Error(`${failed} tests failed`))
}
}
} else {
console.debug(entry.toString())
}
})
})
const loadEvent = new Promise(resolve => {
Page.loadEventFired(e => resolve(e))
})
console.log(`navigate to ${baseURL}/?reporter=tap`)
await Page.navigate({ url: `${baseURL}/?reporter=tap` })
await loadEvent
console.debug('location', (await Runtime.evaluate({ expression: 'window.location.toString()' })).result.value)
setTimeout(async () => {
const evaluation = await Runtime.evaluate({ expression: 'window.mocha.run()' })
if (evaluation.result.subtype === 'error') {
throw new Error('error running mocha')
}
}, 1000)
await finishedLogs
} catch (e) {
console.error(e)
exitStatus = 1
} finally {
console.debug('Shutting down....')
if (client) {
await client.close()
}
await chrome.kill()
process.exit(exitStatus)
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment