-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-browser-server.js
72 lines (62 loc) · 2.04 KB
/
test-browser-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const { expect } = require('chai')
const path = require('path')
const puppeteer = require('puppeteer')
const { Suite } = require('supposed')
const blueprint = require('@polyn/blueprint')
const __projectdir = process.cwd()
const suiteConfig = {
name: '@polyn/blueprint (browser)',
assertionLibrary: expect,
sut: blueprint,
}
const suite = Suite(suiteConfig)
const runner = suite.runner({
title: suiteConfig.name,
directories: ['./src'],
dependencies: ['/node_modules/chai/chai.js', '/dist/blueprint.min.js'],
matchesNamingConvention: /.(\.test\.js)$/i,
matchesIgnoredConvention: /node_modules|documentation.test.js/i,
stringifiedSuiteConfig: '{ reporter: "event", assertionLibrary: chai.expect, inject: { expect: chai.expect, sut: polyn.blueprint } }',
})
const debug = false
const puppeteerConfig = debug
? {
headless: false,
slowMo: 250, // slow down by 250ms
devtools: true,
}
: undefined
const runTestsInBrowser = async (context) => {
const browser = await puppeteer.launch(puppeteerConfig)
const page = await browser.newPage()
page.on('console', async (msg) => {
const txt = msg.text()
try {
const json = JSON.parse(txt)
context.lastEvent = json
suite.config.reporters.forEach((reporter) => reporter.write({
...json,
...{ suiteId: suiteConfig.name },
}))
if (json.type === 'END' && json.totals.failed > 0) {
// maybe print a PDF that someone can review if this is being automated
await page.pdf({ path: path.join(__projectdir, `test-log.${Date.now()}.pdf`), format: 'A4' })
}
} catch (err) {
context.lastEvent = { msg: txt, err }
}
})
await page.goto(`http://localhost:${context.runConfig.port}`, { waitUntil: 'networkidle2' })
if (debug) {
await page.evaluate(() => {
debugger // eslint-disable-line no-debugger
})
}
await browser.close()
return context
}
module.exports = async () => {
const context = await runner.startServer()
const finalContext = await runTestsInBrowser(context)
return finalContext
}