Skip to content

chore: add browser example test #846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,11 @@ jobs:
steps:
- uses: actions/checkout@v2
- run: yarn
- run: cd examples && yarn && npm run test -- chat
- run: cd examples && yarn && npm run test -- chat
test-libp2p-in-the-browser-example:
needs: check
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- run: yarn
- run: cd examples && yarn && npm run test -- libp2p-in-the-browser
1 change: 1 addition & 0 deletions examples/libp2p-in-the-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
],
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "parcel build index.html",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not needed with the new setup

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried this out, but we need it otherwise the tests do not run.

"start": "parcel index.html"
},
"keywords": [],
Expand Down
52 changes: 52 additions & 0 deletions examples/libp2p-in-the-browser/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

const execa = require('execa')
const { chromium } = require('playwright');

async function run() {
let url = ''
const proc = execa('parcel', ['./index.html'], {
preferLocal: true,
localDir: __dirname,
cwd: __dirname,
all: true
})

proc.all.on('data', async (chunk) => {
/**@type {string} */
const out = chunk.toString()

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()
}
}
})

}

module.exports = run
3 changes: 3 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"fs-extra": "^8.1.0",
"p-defer": "^3.0.0",
"which": "^2.0.1"
},
"devDependencies": {
"playwright": "^1.7.1"
}
}
7 changes: 3 additions & 4 deletions examples/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ async function testExample (dir) {
await installDeps(dir)
await build(dir)
await runTest(dir)
// TODO: add browser test setup
}

async function installDeps (dir) {
Expand Down Expand Up @@ -89,7 +88,7 @@ async function runTest (dir) {
return
}

const runTest = require(testFile)
const test = require(testFile)

await runTest()
}
await test()
}