diff --git a/build/testing/browser/index.js b/build/testing/browser/index.js
index 843229c..e6bdc09 100644
--- a/build/testing/browser/index.js
+++ b/build/testing/browser/index.js
@@ -1,11 +1,25 @@
-const puppeteer = require('puppeteer')
+const path = require('path')
-const browserTests = async ({ logWarnings = false, serverPort = 38000, keepServerRunning = false, puppeteerOptions = {} }) => {
+const puppeteer = require('puppeteer')
+const minimatch = require('minimatch')
+const glob = require('glob')
+const rootDir = path.join(__dirname, '../../..')
+
+const browserTests = async (
+ {
+ logWarnings = false,
+ serverPort = 38000,
+ keepServerRunning = false,
+ puppeteerOptions = {
+ headless: false,
+ devtools: true
+ }
+ }, testFiles) => {
const server = require('./server.js').server
- await server.init()
+ await server.init(testFiles)
await server.listen(serverPort)
const browser = await puppeteer.launch(puppeteerOptions)
- const page = await browser.newPage()
+ const page = (await browser.pages())[0]
page.on('console', function (message) {
let ignore = message.type() === 'warning' && !logWarnings
if (message.type() === 'error' && message.location()) {
@@ -39,28 +53,69 @@ const browserTests = async ({ logWarnings = false, serverPort = 38000, keepServe
}
console[consoleType](text, ...args)
})
+
page.on('error', function (err) { page.emit(new Error(err)) })
- await page.goto('http://localhost:38000/')
- const watchDog = page.waitForFunction('_mocha.state === \'stopped\'', { timeout: 0 })
- await watchDog
+ page.on('close', async () => {
+ await close()
+ })
- if (keepServerRunning === false) {
- await page.close()
- await browser.close()
- await server.close()
+ page.goto('http://localhost:38000/').then(async () => {
+ const watchDog = page.waitForFunction('_mocha.state === \'stopped\'', { timeout: 0 })
+ await watchDog.catch(async (reason) => {
+ console.error(reason)
+ })
+ if (puppeteerOptions.headless === true) {
+ await close()
+ }
+ }).catch(async (reason) => {
+ console.error(reason)
+ })
+
+ async function close () {
+ console.log('Closing browser tests...')
+ await browser.close().catch(() => {})
+ if (keepServerRunning !== true) {
+ await server.close().catch(() => {})
+ }
}
}
+function processedTestFiles (testFilesStr) {
+ if (testFilesStr === undefined) {
+ return undefined
+ }
+ // Let us first remove surrounding quotes in string (it gives issues in windows)
+ testFilesStr = testFilesStr.replace(/^['"]/, '').replace(/['"]$/, '')
+ const filenames = glob.sync(testFilesStr, { cwd: rootDir, matchBase: true })
+ if (filenames.length > 0) {
+ filenames.forEach(file => {
+ const isTsTestFile = minimatch(file, '{test/**/*.ts,src/**/*.spec.ts}', { matchBase: true })
+ if (!isTsTestFile) {
+ throw new Error(`test file '${file}' not found`)
+ }
+ })
+ }
+ return filenames
+}
+
const opts = {
// puppeteer options
puppeteerOptions: {
- headless: true
+ headless: false,
+ devtools: true
// slowMo: 100,
// timeout: 10000
},
- doNotLogWarnings: true,
+ logWarnings: false, // log warnings in Node console (usually not needed)
keepServerRunning: false, // keep server running until manually closed with ctrl-c. In combination with puppeteerOptions.headless (or just connecting any browser to the test page) allows debugging in browser
serverPort: 38000
}
-browserTests(opts)
+
+const args = process.argv.slice(2)
+if (args[0] === 'headless') {
+ opts.puppeteerOptions.headless = true
+ args.shift()
+}
+
+browserTests(opts, processedTestFiles(args[0]))
diff --git a/build/testing/browser/server.js b/build/testing/browser/server.js
index 730d627..d777eeb 100644
--- a/build/testing/browser/server.js
+++ b/build/testing/browser/server.js
@@ -4,13 +4,15 @@ const fs = require('fs')
const http = require('http')
const path = require('path')
const pkgJson = require('../../../package.json')
+require('dotenv').config()
const rollup = require('rollup')
const resolve = require('@rollup/plugin-node-resolve').nodeResolve
const replace = require('@rollup/plugin-replace')
const multi = require('@rollup/plugin-multi-entry')
-const typescript = require('@rollup/plugin-typescript')
+const typescriptPlugin = require('@rollup/plugin-typescript')
const commonjs = require('@rollup/plugin-commonjs')
+const json = require('@rollup/plugin-json')
const rootDir = path.join(__dirname, '..', '..', '..')
@@ -41,34 +43,48 @@ const indexHtml = `
+