Save coverage on beforeunload event
This commit is contained in:
parent
300bf24241
commit
016faacf86
1 changed files with 45 additions and 17 deletions
|
@ -63,16 +63,51 @@ exports.generateReport = () => {
|
||||||
reporter.write(collector, true, function () {})
|
reporter.write(collector, true, function () {})
|
||||||
}
|
}
|
||||||
|
|
||||||
const saveCoverageData = (webContents, callback) => {
|
// Save coverage data from the browser window with the given pid
|
||||||
webContents.executeJavaScript('[global.__coverage__, global.process && global.process.pid]', function (results) {
|
const saveCoverageData = (coverage, pid) => {
|
||||||
const coverage = results[0]
|
|
||||||
const pid = results[1]
|
|
||||||
if (coverage && pid) {
|
if (coverage && pid) {
|
||||||
const dataPath = path.join(outputPath, 'data', `${pid}-${Date.now()}.json`)
|
const dataPath = path.join(outputPath, 'data', `${pid}-${Date.now()}.json`)
|
||||||
mkdirp.sync(path.dirname(dataPath))
|
mkdirp.sync(path.dirname(dataPath))
|
||||||
fs.writeFileSync(dataPath, JSON.stringify(coverage, null, 2))
|
fs.writeFileSync(dataPath, JSON.stringify(coverage))
|
||||||
callback()
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const getCoverageFromWebContents = (webContents, callback) => {
|
||||||
|
webContents.executeJavaScript('[global.__coverage__, global.process && global.process.pid]', (results) => {
|
||||||
|
const coverage = results[0]
|
||||||
|
const pid = results[1]
|
||||||
|
callback(coverage, pid)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save coverage data when a BrowserWindow is closed manually
|
||||||
|
const patchBrowserWindow = () => {
|
||||||
|
const {BrowserWindow} = require('electron')
|
||||||
|
|
||||||
|
const {destroy} = BrowserWindow.prototype
|
||||||
|
BrowserWindow.prototype.destroy = function () {
|
||||||
|
if (this.isDestroyed()) return destroy.call(this)
|
||||||
|
getCoverageFromWebContents(this.webContents, (coverage, pid) => {
|
||||||
|
saveCoverageData(coverage, pid)
|
||||||
|
destroy.call(this)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save coverage data when beforeunload fires on the webContent's window object
|
||||||
|
const saveCoverageOnBeforeUnload = () => {
|
||||||
|
const {app, ipcMain} = require('electron')
|
||||||
|
|
||||||
|
ipcMain.on('save-coverage', function (event, coverage, pid) {
|
||||||
|
saveCoverageData(coverage, pid)
|
||||||
|
})
|
||||||
|
|
||||||
|
app.on('web-contents-created', function (event, webContents) {
|
||||||
|
webContents.executeJavaScript(`
|
||||||
|
window.addEventListener('beforeunload', function () {
|
||||||
|
require('electron').ipcRenderer.send('save-coverage', global.__coverage__, process && process.pid)
|
||||||
|
})
|
||||||
|
`)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,13 +116,6 @@ exports.setupCoverage = () => {
|
||||||
if (coverage == null) return
|
if (coverage == null) return
|
||||||
|
|
||||||
rimraf.sync(path.join(outputPath, 'data'))
|
rimraf.sync(path.join(outputPath, 'data'))
|
||||||
|
patchBrowserWindow()
|
||||||
const {BrowserWindow} = require('electron')
|
saveCoverageOnBeforeUnload()
|
||||||
|
|
||||||
const originalDestroy = BrowserWindow.prototype.destroy
|
|
||||||
BrowserWindow.prototype.destroy = function () {
|
|
||||||
saveCoverageData(this.webContents, () => {
|
|
||||||
originalDestroy.call(this)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue