playwright-test
Version:
Run mocha, zora, uvu, tape and benchmark.js scripts inside real browsers with playwright.
61 lines (50 loc) • 1.31 kB
JavaScript
// @ts-nocheck
/* eslint-disable new-cap */
// Run benchmarkjs in the browser https://github.com/bestiejs/benchmark.js/issues/128#issuecomment-271615298
// const process = require('process');
const _ = require('lodash')
require('../vendor/benchmark')
const BenchmarkSpecial = globalThis.Benchmark.runInContext({
_,
process,
})
let runningCount = 0
const signalFinished = () => {
if (runningCount === 0) {
setTimeout(() => {
const options = JSON.parse(process.env.PW_OPTIONS)
// eslint-disable-next-line no-undef
if (options.mode === 'worker') {
postMessage({ pwRunEnded: true })
} else {
globalThis.PW_TEST.end()
}
}, 1000)
}
}
const proxy = new Proxy(BenchmarkSpecial, {
get(obj, prop) {
if (prop === 'Suite') {
const SuiteProxy = new Proxy(obj.Suite, {
construct(target, args) {
const suite = new target(...args)
suite.on('start', () => {
runningCount++
})
suite.on('complete', () => {
runningCount--
signalFinished()
})
return suite
},
})
return SuiteProxy
}
if (prop in obj) {
return obj[prop]
}
},
})
globalThis.Benchmark = proxy
module.exports = proxy
exports.Benchmark = proxy