Skip to content

Commit 09a5f24

Browse files
committed
chore: add browser example test
1 parent 42b51d8 commit 09a5f24

File tree

8 files changed

+195
-4
lines changed

8 files changed

+195
-4
lines changed

.github/workflows/main.yml

+7
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,10 @@ jobs:
6565
- uses: actions/checkout@v2
6666
- run: yarn
6767
- run: cd examples && yarn && npm run test -- auto-relay
68+
test-libp2p-in-the-browser-example:
69+
needs: check
70+
runs-on: macos-latest
71+
steps:
72+
- uses: actions/checkout@v2
73+
- run: yarn
74+
- run: cd examples && yarn && npm run test -- libp2p-in-the-browser

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dist
4040
test/test-data/go-ipfs-repo/LOCK
4141
test/test-data/go-ipfs-repo/LOG
4242
test/test-data/go-ipfs-repo/LOG.old
43+
tests_output/
4344

4445
# while testing npm5
4546
package-lock.json

examples/libp2p-in-the-browser/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"babel-plugin-syntax-async-functions": "^6.13.0",
2929
"babel-plugin-transform-regenerator": "^6.26.0",
3030
"babel-polyfill": "^6.26.0",
31+
"p-retry": "^4.2.0",
3132
"parcel-bundler": "^1.12.4"
3233
}
3334
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const pkg = require('./package.json')
4+
5+
module.exports = {
6+
[pkg.name]: function (browser) {
7+
browser
8+
.url(process.env.LIBP2P_EXAMPLE_TEST_URL)
9+
.waitForElementVisible('#status')
10+
.waitForElementVisible('#output')
11+
.pause(5000)
12+
13+
browser.expect.element('#status').text.to.contain('libp2p started!')
14+
browser.expect.element('#output').text.to.contain('libp2p id is')
15+
16+
browser.expect.element('#output').text.to.contain('Found peer')
17+
browser.expect.element('#output').text.to.contain('Connected to')
18+
19+
browser.end()
20+
}
21+
}

examples/nightwatch.conf.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict'
2+
3+
const { ephemeralPort } = require('./utils')
4+
5+
const WEBRIVER_PORT = ephemeralPort()
6+
7+
// config used to test examples
8+
module.exports = {
9+
src_folders: ['tests'],
10+
11+
webdriver: {
12+
start_process: true,
13+
server_path: 'node_modules/.bin/chromedriver',
14+
port: WEBRIVER_PORT,
15+
cli_args: [
16+
`--port=${WEBRIVER_PORT}`
17+
]
18+
},
19+
20+
test_settings: {
21+
default: {
22+
desiredCapabilities: {
23+
browserName: 'chrome',
24+
chromeOptions: {
25+
args: ['headless']
26+
}
27+
}
28+
}
29+
},
30+
31+
globals: {
32+
asyncHookTimeout: 120000,
33+
waitForConditionTimeout: 60000
34+
}
35+
}

examples/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88
},
99
"license": "MIT",
1010
"dependencies": {
11+
"chromedriver": "^87.0.0",
1112
"execa": "^2.1.0",
1213
"fs-extra": "^8.1.0",
1314
"p-defer": "^3.0.0",
15+
"http-server": "~0.11.1",
16+
"nightwatch": "^1.2.4",
1417
"which": "^2.0.1"
1518
}
1619
}

examples/test.js

+35-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const fs = require('fs-extra')
77
const path = require('path')
88
const execa = require('execa')
99
const dir = path.join(__dirname, process.argv[2])
10+
const { startServer } = require('./utils')
1011

1112
testExample(dir)
1213
.then(() => {}, (err) => {
@@ -21,8 +22,12 @@ testExample(dir)
2122
async function testExample (dir) {
2223
await installDeps(dir)
2324
await build(dir)
24-
await runTest(dir)
25-
// TODO: add browser test setup
25+
26+
if (dir.includes('browser')) {
27+
await runBrowserTest(dir)
28+
} else {
29+
await runNodeTest(dir)
30+
}
2631
}
2732

2833
async function installDeps (dir) {
@@ -80,7 +85,7 @@ async function build (dir) {
8085
await proc
8186
}
8287

83-
async function runTest (dir) {
88+
async function runNodeTest (dir) {
8489
console.info('Running node tests in', dir)
8590
const testFile = path.join(dir, 'test.js')
8691

@@ -92,4 +97,30 @@ async function runTest (dir) {
9297
const runTest = require(testFile)
9398

9499
await runTest()
95-
}
100+
}
101+
102+
async function runBrowserTest (dir) {
103+
console.info('Running browser tests in', dir)
104+
105+
const server = await startServer(dir)
106+
107+
console.info('Running tests at', server.url)
108+
109+
const proc = execa('nightwatch', [ path.join(dir, 'test.js') ], {
110+
cwd: __dirname,
111+
env: {
112+
...process.env,
113+
LIBP2P_EXAMPLE_TEST_URL: server.url
114+
}
115+
})
116+
117+
proc.all.on('data', (data) => {
118+
process.stdout.write(data)
119+
})
120+
121+
try {
122+
await proc
123+
} finally {
124+
server.stop()
125+
}
126+
}

examples/utils.js

+92
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,96 @@
33
const execa = require('execa')
44
const fs = require('fs-extra')
55
const which = require('which')
6+
const path = require('path')
7+
8+
async function startServer (dir) {
9+
async function serveFrom (path) {
10+
return new Promise((resolve, reject) => {
11+
let output = ''
12+
13+
const proc = execa.command(`http-server ${path} -a 127.0.0.1`, {
14+
cwd: __dirname
15+
})
16+
proc.all.on('data', (data) => {
17+
process.stdout.write(data)
18+
19+
const line = data.toString('utf8')
20+
output += line
21+
22+
if (output.includes('Hit CTRL-C to stop the server')) {
23+
// find the port
24+
const port = output.match(/http:\/\/127.0.0.1:(\d+)/)[1]
25+
26+
if (!port) {
27+
throw new Error(`Could not find port in ${output}`)
28+
}
29+
30+
resolve({
31+
stop: () => {
32+
console.info('Stopping server')
33+
proc.kill('SIGINT', {
34+
forceKillAfterTimeout: 2000
35+
})
36+
},
37+
url: `http://127.0.0.1:${port}`
38+
})
39+
}
40+
})
41+
42+
proc.then(() => {}, (err) => reject(err))
43+
})
44+
}
45+
46+
const serverPaths = [
47+
path.join(dir, 'build'),
48+
path.join(dir, 'dist'),
49+
path.join(dir, 'public')
50+
]
51+
52+
for (const p of serverPaths) {
53+
if (fs.existsSync(p)) {
54+
return serveFrom(p)
55+
}
56+
}
57+
58+
// running a bare index.html file
59+
const files = [
60+
path.join(dir, 'index.html')
61+
]
62+
63+
for (const f of files) {
64+
if (fs.existsSync(f)) {
65+
console.info('Found bare file', f)
66+
67+
if (!fs.existsSync(path.resolve(dir, '../../dist'))) {
68+
console.info('Building IPFS')
69+
const proc = execa.command('npm run build', {
70+
cwd: path.resolve(dir, '../../'),
71+
env: {
72+
...process.env,
73+
CI: true // needed for some "clever" build tools
74+
}
75+
})
76+
proc.all.on('data', (data) => {
77+
process.stdout.write(data)
78+
})
79+
80+
await proc
81+
}
82+
83+
return Promise.resolve({
84+
url: `file://${f}`,
85+
stop: () => { }
86+
})
87+
}
88+
}
89+
90+
throw new Error('Browser examples must contain a `public`, `dist` or `build` folder or an `index.html` file')
91+
}
92+
93+
function ephemeralPort (min = 49152, max = 65535) {
94+
return Math.floor(Math.random() * (max - min + 1) + min)
95+
}
696

797
async function isExecutable (command) {
898
try {
@@ -57,5 +147,7 @@ async function waitForOutput (expectedOutput, command, args = [], opts = {}) {
57147
}
58148

59149
module.exports = {
150+
startServer,
151+
ephemeralPort,
60152
waitForOutput
61153
}

0 commit comments

Comments
 (0)