Skip to content

Commit

Permalink
chore: use playwright
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jan 18, 2021
1 parent e83dc7d commit bdf7f4d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 181 deletions.
1 change: 0 additions & 1 deletion examples/libp2p-in-the-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"babel-plugin-syntax-async-functions": "^6.13.0",
"babel-plugin-transform-regenerator": "^6.26.0",
"babel-polyfill": "^6.26.0",
"p-retry": "^4.2.0",
"parcel-bundler": "^1.12.4"
}
}
59 changes: 45 additions & 14 deletions examples/libp2p-in-the-browser/test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
'use strict'

const pkg = require('./package.json')
const execa = require('execa')
const { chromium } = require('playwright');

module.exports = {
[pkg.name]: function (browser) {
browser
.url(process.env.LIBP2P_EXAMPLE_TEST_URL)
.waitForElementVisible('#status')
.waitForElementVisible('#output')
.pause(5000)
async function run() {
let url = ''
const proc = execa('parcel', ['./index.html'], {
preferLocal: true,
localDir: __dirname,
cwd: __dirname,
all: true
})

browser.expect.element('#status').text.to.contain('libp2p started!')
browser.expect.element('#output').text.to.contain('libp2p id is')
proc.all.on('data', async (chunk) => {
/**@type {string} */
const out = chunk.toString()

browser.expect.element('#output').text.to.contain('Found peer')
browser.expect.element('#output').text.to.contain('Connected to')
if (out.includes('Server running at')) {
url = out.replace('Server running at ', '')
}

if (out.includes('✨ Built in ')) {
try {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto(url);
await page.waitForFunction(selector => document.querySelector(selector).innerText === 'libp2p started!', '#status')
await page.waitForFunction(
selector => {
const text = document.querySelector(selector).innerText
return text.includes('libp2p id is') &&
text.includes('Found peer') &&
text.includes('Connected to')
},
'#output',
{ timeout: 5000 }
)
await browser.close();

} catch (err) {
console.error(err)
process.exit(1)
} finally {
proc.cancel()
}
}
})

browser.end()
}
}

module.exports = run
35 changes: 0 additions & 35 deletions examples/nightwatch.conf.js

This file was deleted.

6 changes: 3 additions & 3 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
},
"license": "MIT",
"dependencies": {
"chromedriver": "^87.0.0",
"execa": "^2.1.0",
"fs-extra": "^8.1.0",
"p-defer": "^3.0.0",
"http-server": "~0.11.1",
"nightwatch": "^1.2.4",
"which": "^2.0.1"
},
"devDependencies": {
"playwright": "^1.7.1"
}
}
40 changes: 4 additions & 36 deletions examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const fs = require('fs-extra')
const path = require('path')
const execa = require('execa')
const dir = path.join(__dirname, process.argv[2])
const { startServer } = require('./utils')

testExample(dir)
.then(() => {}, (err) => {
Expand All @@ -22,12 +21,7 @@ testExample(dir)
async function testExample (dir) {
await installDeps(dir)
await build(dir)

if (dir.includes('browser')) {
await runBrowserTest(dir)
} else {
await runNodeTest(dir)
}
await runTest(dir)
}

async function installDeps (dir) {
Expand Down Expand Up @@ -85,7 +79,7 @@ async function build (dir) {
await proc
}

async function runNodeTest (dir) {
async function runTest (dir) {
console.info('Running node tests in', dir)
const testFile = path.join(dir, 'test.js')

Expand All @@ -94,33 +88,7 @@ async function runNodeTest (dir) {
return
}

const runTest = require(testFile)

await runTest()
}

async function runBrowserTest (dir) {
console.info('Running browser tests in', dir)

const server = await startServer(dir)
const test = require(testFile)

console.info('Running tests at', server.url)

const proc = execa('nightwatch', [ path.join(dir, 'test.js') ], {
cwd: __dirname,
env: {
...process.env,
LIBP2P_EXAMPLE_TEST_URL: server.url
}
})

proc.all.on('data', (data) => {
process.stdout.write(data)
})

try {
await proc
} finally {
server.stop()
}
await test()
}
92 changes: 0 additions & 92 deletions examples/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,6 @@
const execa = require('execa')
const fs = require('fs-extra')
const which = require('which')
const path = require('path')

async function startServer (dir) {
async function serveFrom (path) {
return new Promise((resolve, reject) => {
let output = ''

const proc = execa.command(`http-server ${path} -a 127.0.0.1`, {
cwd: __dirname
})
proc.all.on('data', (data) => {
process.stdout.write(data)

const line = data.toString('utf8')
output += line

if (output.includes('Hit CTRL-C to stop the server')) {
// find the port
const port = output.match(/http:\/\/127.0.0.1:(\d+)/)[1]

if (!port) {
throw new Error(`Could not find port in ${output}`)
}

resolve({
stop: () => {
console.info('Stopping server')
proc.kill('SIGINT', {
forceKillAfterTimeout: 2000
})
},
url: `http://127.0.0.1:${port}`
})
}
})

proc.then(() => {}, (err) => reject(err))
})
}

const serverPaths = [
path.join(dir, 'build'),
path.join(dir, 'dist'),
path.join(dir, 'public')
]

for (const p of serverPaths) {
if (fs.existsSync(p)) {
return serveFrom(p)
}
}

// running a bare index.html file
const files = [
path.join(dir, 'index.html')
]

for (const f of files) {
if (fs.existsSync(f)) {
console.info('Found bare file', f)

if (!fs.existsSync(path.resolve(dir, '../../dist'))) {
console.info('Building IPFS')
const proc = execa.command('npm run build', {
cwd: path.resolve(dir, '../../'),
env: {
...process.env,
CI: true // needed for some "clever" build tools
}
})
proc.all.on('data', (data) => {
process.stdout.write(data)
})

await proc
}

return Promise.resolve({
url: `file://${f}`,
stop: () => { }
})
}
}

throw new Error('Browser examples must contain a `public`, `dist` or `build` folder or an `index.html` file')
}

function ephemeralPort (min = 49152, max = 65535) {
return Math.floor(Math.random() * (max - min + 1) + min)
}

async function isExecutable (command) {
try {
Expand Down Expand Up @@ -147,7 +57,5 @@ async function waitForOutput (expectedOutput, command, args = [], opts = {}) {
}

module.exports = {
startServer,
ephemeralPort,
waitForOutput
}

0 comments on commit bdf7f4d

Please sign in to comment.