| 1 |
/* eslint-disable no-console */ |
| 2 |
/* -------------------------------------------------------------------------------------------- |
| 3 |
* Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 |
* Licensed under the MIT License. See License.txt in the project root for license information. |
| 5 |
* ------------------------------------------------------------------------------------------ */ |
| 6 |
'use strict'; |
| 7 |
//@ts-check |
| 8 |
|
| 9 |
const path = require('path'); |
| 10 |
const url = require('url'); |
| 11 |
const events = require('events'); |
| 12 |
|
| 13 |
const mocha = require('mocha'); |
| 14 |
const playwright = require('playwright'); |
| 15 |
const httpServer = require('http-server'); |
| 16 |
|
| 17 |
class EchoRunner extends events.EventEmitter { |
| 18 |
|
| 19 |
constructor(event, title = '') { |
| 20 |
super(); |
| 21 |
event.on('start', () => this.emit('start')); |
| 22 |
event.on('end', () => this.emit('end')); |
| 23 |
event.on('suite', (suite) => this.emit('suite', EchoRunner.deserializeSuite(suite, title))); |
| 24 |
event.on('suite end', (suite) => this.emit('suite end', EchoRunner.deserializeSuite(suite, title))); |
| 25 |
event.on('test', (test) => this.emit('test', EchoRunner.deserializeRunnable(test))); |
| 26 |
event.on('test end', (test) => this.emit('test end', EchoRunner.deserializeRunnable(test))); |
| 27 |
event.on('hook', (hook) => this.emit('hook', EchoRunner.deserializeRunnable(hook))); |
| 28 |
event.on('hook end', (hook) => this.emit('hook end', EchoRunner.deserializeRunnable(hook))); |
| 29 |
event.on('pass', (test) => this.emit('pass', EchoRunner.deserializeRunnable(test))); |
| 30 |
event.on('fail', (test, err) => this.emit('fail', EchoRunner.deserializeRunnable(test, title), EchoRunner.deserializeError(err))); |
| 31 |
event.on('pending', (test) => this.emit('pending', EchoRunner.deserializeRunnable(test))); |
| 32 |
} |
| 33 |
|
| 34 |
static deserializeSuite(suite, titleExtra) { |
| 35 |
return { |
| 36 |
root: suite.root, |
| 37 |
suites: suite.suites, |
| 38 |
tests: suite.tests, |
| 39 |
title: titleExtra && suite.title ? `${suite.title} - /${titleExtra}/` : suite.title, |
| 40 |
fullTitle: () => suite.fullTitle, |
| 41 |
timeout: () => suite.timeout, |
| 42 |
retries: () => suite.retries, |
| 43 |
enableTimeouts: () => suite.enableTimeouts, |
| 44 |
slow: () => suite.slow, |
| 45 |
bail: () => suite.bail |
| 46 |
}; |
| 47 |
} |
| 48 |
|
| 49 |
static deserializeRunnable(runnable, titleExtra) { |
| 50 |
return { |
| 51 |
title: runnable.title, |
| 52 |
fullTitle: () => titleExtra && runnable.fullTitle ? `${runnable.fullTitle} - /${titleExtra}/` : runnable.fullTitle, |
| 53 |
async: runnable.async, |
| 54 |
slow: () => runnable.slow, |
| 55 |
speed: runnable.speed, |
| 56 |
duration: runnable.duration |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
static deserializeError(err) { |
| 61 |
const inspect = err.inspect; |
| 62 |
err.inspect = () => inspect; |
| 63 |
return err; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
async function runTests(location) { |
| 69 |
return new Promise((resolve, reject) => { |
| 70 |
const root = path.join(__dirname, '..', '..'); |
| 71 |
const server = httpServer.createServer({ |
| 72 |
root: root, showDir: true, |
| 73 |
cors: true, |
| 74 |
headers: { |
| 75 |
'Cross-Origin-Opener-Policy': 'same-origin', |
| 76 |
'Cross-Origin-Embedder-Policy': 'require-corp' |
| 77 |
} |
| 78 |
}); |
| 79 |
server.listen(8080, '127.0.0.1', async () => { |
| 80 |
let failCount = 0; |
| 81 |
const browser = await playwright['chromium'].launch({ headless: true, devtools: false }); |
| 82 |
const context = await browser.newContext(); |
| 83 |
const page = await context.newPage(); |
| 84 |
const emitter = new events.EventEmitter(); |
| 85 |
emitter.on('fail', () => { |
| 86 |
failCount++; |
| 87 |
}); |
| 88 |
emitter.on('end', async () => { |
| 89 |
process.exitCode = failCount === 0 ? 0 : 1; |
| 90 |
await browser.close(); |
| 91 |
server.close((err) => { |
| 92 |
if (err) { |
| 93 |
reject(err); |
| 94 |
} else { |
| 95 |
resolve(); |
| 96 |
} |
| 97 |
}); |
| 98 |
}); |
| 99 |
const echoRunner = new EchoRunner(emitter, 'Chromium'); |
| 100 |
if (process.platform === 'win32') { |
| 101 |
new mocha.reporters.List(echoRunner); |
| 102 |
} else { |
| 103 |
new mocha.reporters.Spec(echoRunner); |
| 104 |
} |
| 105 |
await page.exposeFunction('mocha_report', (type, data1, data2) => { |
| 106 |
emitter.emit(type, data1, data2); |
| 107 |
}); |
| 108 |
|
| 109 |
const target = new url.URL(location); |
| 110 |
page.goto(target.href); |
| 111 |
}); |
| 112 |
}); |
| 113 |
} |
| 114 |
|
| 115 |
runTests(process.argv[2]); |