Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 50f3667

Browse files
authored
test: Test examples (#2528)
* test: adds automated tests to browser examples * chore: upgrade chrome * chore: throw on unhandled rejection * fix: fail if FAILED is in output * chore: exit with same exit code as nightwatch * chore: fix element id * test: add assertion * chore: add video streaming test * chore: change dist url * chore: build IPFS for basic browser demos * test: add some node tests * test: adds some more tests. ethereum one is a little wobbly * chore: accept bundle as build script * test: add final few tests * test: control assertion timeouts with global * test: wait for other peer to join workspace before sending file * chore: tidy up deps
1 parent c386b5c commit 50f3667

File tree

48 files changed

+1700
-48
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1700
-48
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ logs
88
*.log
99
# npm pack
1010
*.tgz
11+
**/tests_output
1112

1213
coverage
1314

.travis.yml

+157-9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ addons:
3434
# Fixes error while loading shared libraries: libgconf-2.so.4: cannot open shared object file: No such file or directory
3535
# https://github.com/electron/electron/issues/1518
3636
- libgconf-2-4
37+
# Ensure chrome is the latest version
38+
# https://stackoverflow.com/questions/57903415/travis-ci-chrome-62-instead-of-77
39+
- dpkg
40+
chrome: stable
3741

3842
script: npx nyc -s npx aegir test -t node --timeout 10000 --bail
3943
after_success:
@@ -74,61 +78,205 @@ jobs:
7478
- npx aegir test -t electron-renderer --bail --timeout 10000
7579

7680
- stage: test
77-
name: interop node
81+
name: interop - node
7882
script:
7983
- cd node_modules/ipfs-interop
8084
- IPFS_JS_EXEC=./../../src/cli/bin.js IPFS_REUSEPORT=false npx aegir test -t node --bail
8185

8286
- stage: test
83-
name: interop browser
87+
name: interop - browser
8488
script:
8589
- cd node_modules/ipfs-interop
8690
- IPFS_JS_EXEC=./../../src/cli/bin.js IPFS_REUSEPORT=false npx aegir test -t browser --bail
8791

8892
- stage: test
89-
name: interop electron-main
93+
name: interop - electron-main
9094
os: osx
9195
script:
9296
- cd node_modules/ipfs-interop
9397
- IPFS_JS_EXEC=./../../src/cli/bin.js IPFS_REUSEPORT=false npx aegir test -t electron-main -f ./test/node.js --bail --timeout 10000
9498

9599
- stage: test
96-
name: interop electron-renderer
100+
name: interop - electron-renderer
97101
os: osx
98102
script:
99103
- cd node_modules/ipfs-interop
100104
- IPFS_JS_EXEC=./../../src/cli/bin.js IPFS_REUSEPORT=false npx aegir test -t electron-renderer -f ./test/browser.js --bail --timeout 10000
101105

102106
- stage: test
103107
if: branch =~ /^release\/.*$/
104-
name: ipfs-companion
108+
name: external - ipfs-companion
105109
script:
106110
- npm run test:external -- ipfs-companion https://github.com/ipfs-shipyard/ipfs-companion.git
107111

108112
- stage: test
109113
if: branch =~ /^release\/.*$/
110-
name: npm-on-ipfs
114+
name: external - npm-on-ipfs
111115
script:
112116
- npm run test:external -- npm-on-ipfs https://github.com/ipfs-shipyard/npm-on-ipfs.git
113117

114118
- stage: test
115119
if: branch =~ /^release\/.*$/
116-
name: ipfs-pubsub-room
120+
name: external - ipfs-pubsub-room
117121
script:
118122
- npm run test:external -- ipfs-pubsub-room https://github.com/ipfs-shipyard/ipfs-pubsub-room.git
119123

120124
- stage: test
121125
if: branch =~ /^release\/.*$/
122-
name: peer-base
126+
name: external - peer-base
123127
script:
124128
- npm run test:external -- peer-base https://github.com/peer-base/peer-base.git
125129

126130
- stage: test
127131
if: branch =~ /^release\/.*$/
128-
name: service-worker-gateway
132+
name: external - service-worker-gateway
129133
script:
130134
- npm run test:external -- service-worker-gateway https://github.com/ipfs-shipyard/service-worker-gateway.git
131135

136+
- stage: test
137+
if: branch =~ /^release\/.*$/
138+
name: example - browser-add-readable-stream
139+
script:
140+
- cd examples
141+
- npm install
142+
- npm run test -- browser-add-readable-stream
143+
144+
- stage: test
145+
if: branch =~ /^release\/.*$/
146+
name: example - browser-browserify
147+
script:
148+
- cd examples
149+
- npm install
150+
- npm run test -- browser-browserify
151+
152+
- stage: test
153+
if: branch =~ /^release\/.*$/
154+
name: example - browser-create-react-app
155+
script:
156+
- cd examples
157+
- npm install
158+
- npm run test -- browser-create-react-app
159+
160+
- stage: test
161+
if: branch =~ /^release\/.*$/
162+
name: example - browser-mfs
163+
script:
164+
- cd examples
165+
- npm install
166+
- npm run test -- browser-mfs
167+
168+
- stage: test
169+
if: branch =~ /^release\/.*$/
170+
name: example - browser-parceljs
171+
script:
172+
- cd examples
173+
- npm install
174+
- npm run test -- browser-parceljs
175+
176+
- stage: test
177+
if: branch =~ /^release\/.*$/
178+
name: example - browser-readablestream
179+
script:
180+
- cd examples
181+
- npm install
182+
- npm run test -- browser-readablestream
183+
184+
- stage: test
185+
if: branch =~ /^release\/.*$/
186+
name: example - browser-script-tag
187+
script:
188+
- cd examples
189+
- npm install
190+
- npm run test -- browser-script-tag
191+
192+
- stage: test
193+
if: branch =~ /^release\/.*$/
194+
name: example - browser-video-streaming
195+
script:
196+
- cd examples
197+
- npm install
198+
- npm run test -- browser-video-streaming
199+
200+
- stage: test
201+
if: branch =~ /^release\/.*$/
202+
name: example - browser-vue
203+
script:
204+
- cd examples
205+
- npm install
206+
- npm run test -- browser-vue
207+
208+
- stage: test
209+
if: branch =~ /^release\/.*$/
210+
name: example - browser-webpack
211+
script:
212+
- cd examples
213+
- npm install
214+
- npm run test -- browser-webpack
215+
216+
- stage: test
217+
if: branch =~ /^release\/.*$/
218+
name: example - circuit-relaying
219+
script:
220+
- cd examples
221+
- npm install
222+
- npm run test -- circuit-relaying
223+
224+
- stage: test
225+
if: branch =~ /^release\/.*$/
226+
name: example - custom-ipfs-repo
227+
script:
228+
- cd examples
229+
- npm install
230+
- npm run test -- custom-ipfs-repo
231+
232+
- stage: test
233+
if: branch =~ /^release\/.*$/
234+
name: example - custom-libp2p
235+
script:
236+
- cd examples
237+
- npm install
238+
- npm run test -- custom-libp2p
239+
240+
- stage: test
241+
if: branch =~ /^release\/.*$/
242+
name: example - exchange-files-in-browser
243+
script:
244+
- cd examples
245+
- npm install
246+
- npm run test -- exchange-files-in-browser
247+
248+
- stage: test
249+
if: branch =~ /^release\/.*$/
250+
name: example - explore-ethereum-blockchain
251+
script:
252+
- cd examples
253+
- npm install
254+
- npm run test -- explore-ethereum-blockchain
255+
256+
- stage: test
257+
if: branch =~ /^release\/.*$/
258+
name: example - ipfs-101
259+
script:
260+
- cd examples
261+
- npm install
262+
- npm run test -- ipfs-101
263+
264+
- stage: test
265+
if: branch =~ /^release\/.*$/
266+
name: example - running-multiple-nodes
267+
script:
268+
- cd examples
269+
- npm install
270+
- npm run test -- running-multiple-nodes
271+
272+
- stage: test
273+
if: branch =~ /^release\/.*$/
274+
name: example - traverse-ipld-graphs
275+
script:
276+
- cd examples
277+
- npm install
278+
- npm run test -- traverse-ipld-graphs
279+
132280
- stage: tag
133281
# only run on changes to master
134282
if: branch = master AND type = push AND fork = false

examples/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,27 @@ In this section, you will find explanations to different pieces of IPFS Architec
4949
- Storing and Retrieving blocks (soon™)
5050
- IPLD (InterPlanetary Linked-Data) (soon™)
5151
- IPFS Networking - Managing your swarm, libp2p and more (soon™)
52+
53+
## Adding new examples
54+
55+
All examples should include a `test.js` file. The implementation will differ depending on if your example is intended to run under node or the browser.
56+
57+
### Browser `test.js`
58+
59+
All browser examples should either be a whole webapp or a simple demo (e.g. just `index.html` and `index.js` files).
60+
61+
#### Webapp
62+
63+
Webapps must have a `package.json` that contains either a `build` or a `bundle` script. This script should place all assets in a folder named `dist`, `build` or `public` including an `index.html` file. During testing this will have a web server run over it using `http-server`.
64+
65+
#### Simple demo
66+
67+
Simple demos must have an `index.html` file in the root of the demo folder. This file will be loaded in a browser and tested via a `file://` URL.
68+
69+
#### Browser `test.js`
70+
71+
Your `test.js` file should contain a [Nightwatch](https://www.npmjs.com/package/nightwatch) scenario that will be executed. The URL to example will be present in the `IPFS_EXAMPLE_TEST_URL` environmental variable when the test is run.
72+
73+
### Node
74+
75+
Your `test.js` file should export an async function that sets up your test, runs any assertions, tears down anything set up and throws an error if the test fails.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
<!doctype html>
12
<html>
3+
<head>
4+
<title>Add readable stream</title>
5+
</head>
26
<body>
37
<pre id="output"></pre>
4-
<script src="https://unpkg.com/ipfs/dist/index.js"></script>
8+
<script src="../../dist/index.js"></script>
59
<script src="index.js"></script>
610
</body>
711
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
module.exports = {
4+
'Add readable stream example': function (browser) {
5+
browser
6+
.url(process.env.IPFS_EXAMPLE_TEST_URL)
7+
.waitForElementVisible('#output')
8+
9+
browser.expect.element('#output').text.to.contain('directory/ QmVgJePRxp1vhRxDcJWdmuFGfUB5S5RYTtG1NR3bQM4BBn')
10+
11+
browser.end()
12+
}
13+
}

examples/browser-browserify/package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
"keywords": [],
1212
"license": "MIT",
1313
"devDependencies": {
14-
"ipfs": "file:../../",
1514
"browserify": "^16.2.3",
1615
"concat-stream": "^2.0.0",
17-
"http-server": "~0.11.1"
16+
"http-server": "~0.11.1",
17+
"ipfs": "file:../../"
1818
},
19-
"dependencies": {}
19+
"dependencies": {},
20+
"browser": {
21+
"ipfs": "ipfs/dist"
22+
}
2023
}

examples/browser-browserify/public/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
</head>
1515
<body>
1616
<h1>JS IPFS - Add data to IPFS from the browser</h1>
17-
<textarea id="source"></textarea>
18-
<button id="store">add to ipfs</button>
19-
<div>
17+
<textarea id="source" placeholder="Enter some text here"></textarea>
18+
<button id="store">Add text to ipfs</button>
19+
<div id="output" style="display: none">
2020
<div>found in ipfs:</div>
2121
<div class="content" id="hash">[ipfs hash]</div>
2222
<div class="content" id="content">[ipfs content]</div>

examples/browser-browserify/src/index.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,23 @@ document.addEventListener('DOMContentLoaded', async () => {
99

1010
async function store () {
1111
const toStore = document.getElementById('source').value
12+
const result = await node.add(toStore)
1213

13-
const res = await node.add(toStore)
14-
15-
res.forEach((file) => {
14+
for (const file of result) {
1615
if (file && file.hash) {
1716
console.log('successfully stored', file.hash)
18-
display(file.hash)
17+
18+
await display(file.hash)
1919
}
20-
})
20+
}
2121
}
2222

2323
async function display (hash) {
24-
// buffer: true results in the returned result being a buffer rather than a stream
2524
const data = await node.cat(hash)
25+
2626
document.getElementById('hash').innerText = hash
2727
document.getElementById('content').innerText = data
28+
document.getElementById('output').setAttribute('style', 'display: block')
2829
}
2930

3031
document.getElementById('store').onclick = store

examples/browser-browserify/test.js

+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.IPFS_EXAMPLE_TEST_URL)
9+
.waitForElementVisible('#source')
10+
.setValue('#source', 'hello')
11+
.waitForElementVisible('#store')
12+
.pause(1000)
13+
.click('#store')
14+
.waitForElementVisible('#output')
15+
16+
browser.expect.element('#hash').text.to.contain('QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX')
17+
browser.expect.element('#content').text.to.contain('hello')
18+
19+
browser.end()
20+
}
21+
}

examples/browser-create-react-app/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"ipfs-css": "^0.13.1",
99
"react": "^16.8.0",
1010
"react-dom": "^16.8.0",
11-
"react-scripts": "3.1.1",
11+
"react-scripts": "^3.2.0",
1212
"tachyons": "^4.11.1"
1313
},
1414
"scripts": {

examples/browser-create-react-app/src/App.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ const IpfsId = (props) => {
3636
if (!props) return null
3737
return (
3838
<section className='bg-snow mw7 center mt5'>
39-
<h1 className='f3 fw4 ma0 pv3 aqua montserrat tc'>Connected to IPFS</h1>
39+
<h1 className='f3 fw4 ma0 pv3 aqua montserrat tc' data-test='title'>Connected to IPFS</h1>
4040
<div className='pa4'>
4141
{['id', 'agentVersion'].map((key) => (
4242
<div className='mb4' key={key}>
4343
<Title>{key}</Title>
44-
<div className='bg-white pa2 br2 truncate monospace'>{props[key]}</div>
44+
<div className='bg-white pa2 br2 truncate monospace' data-test={key}>{props[key]}</div>
4545
</div>
4646
))}
4747
</div>

0 commit comments

Comments
 (0)