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

Commit 220d1f0

Browse files
authored
chore: remove try-catch from tests where functions are async (#2531)
* chore: remove try-catch from tests where functions are async We had a few instances in the tests where assertions may be missed due to functions not throwing where we thought they would so this PR refactors that code to use `.then(onResult, onReject)` instead. Follows on from #2514 (comment)
1 parent 50f3667 commit 220d1f0

File tree

13 files changed

+173
-269
lines changed

13 files changed

+173
-269
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@
173173
"pull-defer": "~0.2.3",
174174
"pull-file": "^1.1.0",
175175
"pull-mplex": "~0.1.1",
176-
"pull-ndjson": "~0.1.1",
176+
"pull-ndjson": "^0.2.0",
177177
"pull-pushable": "^2.2.0",
178178
"pull-sort": "^1.0.1",
179179
"pull-stream": "^3.6.14",

src/core/components/pubsub.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ module.exports = function pubsub (self) {
3333
return
3434
}
3535

36-
checkOnlineAndEnabled()
36+
try {
37+
checkOnlineAndEnabled()
38+
} catch (err) {
39+
return Promise.reject(err)
40+
}
3741

3842
return self.libp2p.pubsub.subscribe(topic, handler, options)
3943
},
@@ -50,7 +54,11 @@ module.exports = function pubsub (self) {
5054
return
5155
}
5256

53-
checkOnlineAndEnabled()
57+
try {
58+
checkOnlineAndEnabled()
59+
} catch (err) {
60+
return Promise.reject(err)
61+
}
5462

5563
return self.libp2p.pubsub.unsubscribe(topic, handler)
5664
},

src/http/api/resources/files-regular.js

+6
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ exports.refs = {
336336
const unique = request.query.unique
337337
const maxDepth = request.query['max-depth']
338338

339+
// have to do this here otherwise the validation error appears in the stream tail and
340+
// this doesn't work in browsers: https://github.com/ipfs/js-ipfs/issues/2519
341+
if (edges && format !== Format.default) {
342+
throw Boom.badRequest('Cannot set edges to true and also specify format')
343+
}
344+
339345
const source = ipfs.refsPullStream(key, { recursive, format, edges, unique, maxDepth })
340346
return sendRefsReplyStream(request, h, `refs for ${key}`, source)
341347
}

test/cli/daemon.js

+40-63
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,12 @@ describe('daemon', () => {
104104
}
105105
})
106106

107-
try {
108-
await daemon
109-
throw new Error('Did not kill process')
110-
} catch (err) {
111-
expect(err.killed).to.be.true()
112-
113-
expect(stdout).to.include('Daemon is ready')
114-
}
107+
await expect(daemon)
108+
.to.eventually.be.rejected()
109+
.and.to.include({
110+
killed: true
111+
})
112+
.and.to.have.property('stdout').that.includes('Daemon is ready')
115113
})
116114

117115
it('should allow bind to multiple addresses for API and Gateway', async function () {
@@ -142,15 +140,14 @@ describe('daemon', () => {
142140
}
143141
})
144142

145-
try {
146-
await daemon
147-
throw new Error('Did not kill process')
148-
} catch (err) {
149-
expect(err.killed).to.be.true()
143+
const err = await expect(daemon)
144+
.to.eventually.be.rejected()
145+
.and.to.include({
146+
killed: true
147+
})
150148

151-
apiAddrs.forEach(addr => expect(err.stdout).to.include(`API listening on ${addr.slice(0, -2)}`))
152-
gatewayAddrs.forEach(addr => expect(err.stdout).to.include(`Gateway (read only) listening on ${addr.slice(0, -2)}`))
153-
}
149+
apiAddrs.forEach(addr => expect(err.stdout).to.include(`API listening on ${addr.slice(0, -2)}`))
150+
gatewayAddrs.forEach(addr => expect(err.stdout).to.include(`Gateway (read only) listening on ${addr.slice(0, -2)}`))
154151
})
155152

156153
it('should allow no bind addresses for API and Gateway', async function () {
@@ -171,15 +168,12 @@ describe('daemon', () => {
171168
}
172169
})
173170

174-
try {
175-
await daemon
176-
throw new Error('Did not kill process')
177-
} catch (err) {
178-
expect(err.killed).to.be.true()
179-
180-
expect(err.stdout).to.not.include('API listening on')
181-
expect(err.stdout).to.not.include('Gateway (read only) listening on')
182-
}
171+
await expect(daemon)
172+
.to.eventually.be.rejected()
173+
.and.to.include({
174+
killed: true
175+
})
176+
.and.have.property('stdout').that.does.not.include(/(API|Gateway \(read only\)) listening on/g)
183177
})
184178

185179
skipOnWindows('should handle SIGINT gracefully', async function () {
@@ -211,32 +205,17 @@ describe('daemon', () => {
211205
await ipfs('init')
212206

213207
const daemon = ipfs('daemon --silent')
214-
const stop = async (err) => {
215-
daemon.kill()
216208

217-
if (err) {
218-
throw err
219-
}
209+
setTimeout(() => {
210+
daemon.kill('SIGKILL')
211+
}, 5 * 1000)
220212

221-
try {
222-
await daemon
223-
} catch (err) {
224-
if (!err.killed) {
225-
throw err
226-
}
227-
}
228-
}
229-
230-
return new Promise((resolve, reject) => {
231-
daemon.stdout.on('data', (data) => {
232-
reject(new Error('Output was received ' + data.toString('utf8')))
213+
await expect(daemon)
214+
.to.eventually.be.rejected()
215+
.and.to.include({
216+
killed: true,
217+
stdout: ''
233218
})
234-
235-
setTimeout(() => {
236-
resolve()
237-
}, 5 * 1000)
238-
})
239-
.then(stop, stop)
240219
})
241220

242221
it('should present ipfs path help when option help is received', async function () {
@@ -262,16 +241,15 @@ describe('daemon', () => {
262241
}
263242
})
264243

265-
try {
266-
await daemon
267-
throw new Error('Did not kill process')
268-
} catch (err) {
269-
expect(err.killed).to.be.true()
244+
const err = await expect(daemon)
245+
.to.eventually.be.rejected()
246+
.and.to.include({
247+
killed: true
248+
})
270249

271-
expect(err.stdout).to.include(`js-ipfs version: ${pkg.version}`)
272-
expect(err.stdout).to.include(`System version: ${os.arch()}/${os.platform()}`)
273-
expect(err.stdout).to.include(`Node.js version: ${process.versions.node}`)
274-
}
250+
expect(err.stdout).to.include(`js-ipfs version: ${pkg.version}`)
251+
expect(err.stdout).to.include(`System version: ${os.arch()}/${os.platform()}`)
252+
expect(err.stdout).to.include(`Node.js version: ${process.versions.node}`)
275253
})
276254

277255
it('should init by default', async function () {
@@ -290,12 +268,11 @@ describe('daemon', () => {
290268
}
291269
})
292270

293-
try {
294-
await daemon
295-
throw new Error('Did not kill process')
296-
} catch (err) {
297-
expect(err.killed).to.be.true()
298-
}
271+
await expect(daemon)
272+
.to.eventually.be.rejected()
273+
.and.to.include({
274+
killed: true
275+
})
299276

300277
expect(fs.existsSync(repoPath)).to.be.true()
301278
})

test/cli/init.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ describe('init', function () {
8383
it('profile non-existent', async function () {
8484
this.timeout(40 * 1000)
8585

86-
try {
87-
await ipfs('init --profile doesnt-exist')
88-
} catch (err) {
89-
expect(err.stdout).includes('Could not find profile')
90-
}
86+
await expect(ipfs('init --profile doesnt-exist'))
87+
.to.eventually.be.rejected()
88+
.and.to.have.property('stdout').that.includes('Could not find profile')
9189
})
9290

9391
it('should present ipfs path help when option help is received', async function () {

test/core/create-node.spec.js

+7-13
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,14 @@ describe('create node', function () {
171171

172172
expect(ipfs.isOnline()).to.be.false()
173173

174-
try {
175-
await ipfs.ready
176-
} catch (err) {
177-
expect(ipfs.isOnline()).to.be.false()
178-
179-
// After the error has occurred, it should still reject
180-
try {
181-
await ipfs.ready
182-
} catch (_) {
183-
return
184-
}
185-
}
174+
await expect(ipfs.ready)
175+
.to.eventually.be.rejected()
176+
177+
expect(ipfs.isOnline()).to.be.false()
186178

187-
throw new Error('ready promise did not reject')
179+
// After the error has occurred, it should still reject
180+
await expect(ipfs.ready)
181+
.to.eventually.be.rejected()
188182
})
189183

190184
it('should create a ready node with IPFS.create', async () => {

test/core/dht.spec.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,10 @@ describe.skip('dht', () => {
7474
})
7575

7676
describe('put', () => {
77-
it('should callback with error for DHT not available', async () => {
78-
let res
79-
try {
80-
res = await ipfs.dht.put(Buffer.from('a'), Buffer.from('b'))
81-
} catch (err) {
82-
expect(err).to.exist()
83-
expect(err.code).to.equal('ERR_DHT_DISABLED')
84-
}
85-
86-
expect(res).to.not.exist()
77+
it('should error when DHT not available', async () => {
78+
await expect(ipfs.dht.put(Buffer.from('a'), Buffer.from('b')))
79+
.to.eventually.be.rejected()
80+
.and.to.have.property('code', 'ERR_DHT_DISABLED')
8781
})
8882
})
8983
})

test/core/gc.spec.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ const IPFSFactory = require('ipfsd-ctl')
77
const pEvent = require('p-event')
88
const env = require('ipfs-utils/src/env')
99
const IPFS = require('../../src/core')
10-
const { Errors } = require('interface-datastore')
1110

1211
// We need to detect when a readLock or writeLock is requested for the tests
1312
// so we override the Mutex class to emit an event
@@ -189,11 +188,11 @@ describe('gc', function () {
189188
await rm1
190189

191190
// Second rm should fail because GC has already removed that block
192-
try {
193-
await rm2
194-
} catch (err) {
195-
expect(err.code).eql(Errors.dbDeleteFailedError().code)
196-
}
191+
const results = await rm2
192+
const result = results
193+
.filter(result => result.hash === cid2.toString())
194+
.pop()
195+
expect(result).to.have.property('error').that.contains('block not found')
197196

198197
// Confirm second block has been removed
199198
const localRefs = (await ipfs.refs.local()).map(r => r.ref)

test/core/libp2p.spec.js

-36
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const Multiplex = require('pull-mplex')
1111
const SECIO = require('libp2p-secio')
1212
const KadDHT = require('libp2p-kad-dht')
1313
const Libp2p = require('libp2p')
14-
const isNode = require('detect-node')
1514

1615
const libp2pComponent = require('../../src/core/components/libp2p')
1716

@@ -315,40 +314,5 @@ describe('libp2p customization', function () {
315314
done()
316315
})
317316
})
318-
319-
it('select floodsub as pubsub router if node', (done) => {
320-
const ipfs = {
321-
_repo: {
322-
datastore
323-
},
324-
_peerInfo: peerInfo,
325-
_peerBook: peerBook,
326-
// eslint-disable-next-line no-console
327-
_print: console.log,
328-
_options: {}
329-
}
330-
const customConfig = {
331-
...testConfig,
332-
Pubsub: {
333-
Router: 'floodsub'
334-
}
335-
}
336-
337-
try {
338-
_libp2p = libp2pComponent(ipfs, customConfig)
339-
} catch (err) {
340-
if (!isNode) {
341-
expect(err).to.exist()
342-
expect(err.code).to.eql('ERR_NOT_SUPPORTED')
343-
done()
344-
}
345-
}
346-
347-
_libp2p.start((err) => {
348-
expect(err).to.not.exist()
349-
expect(_libp2p._modules.pubsub).to.eql(require('libp2p-floodsub'))
350-
done()
351-
})
352-
})
353317
})
354318
})

test/core/name-pubsub.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ describe('name-pubsub', function () {
106106
const keys = ipns.getIdKeys(fromB58String(idA.id))
107107
const topic = `${namespace}${base64url.encode(keys.routingKey.toBuffer())}`
108108

109-
await nodeB.name.resolve(idA.id)
110-
.then(() => expect.fail('should not have been able to resolve idA.id'), (err) => expect(err).to.exist())
109+
await expect(nodeB.name.resolve(idA.id))
110+
.to.eventually.be.rejected()
111+
.and.to.have.property('code', 'ERR_NO_RECORD_FOUND')
111112

112113
await waitForPeerToSubscribe(nodeA, topic)
113114
await nodeB.pubsub.subscribe(topic, checkMessage)
@@ -128,11 +129,9 @@ describe('name-pubsub', function () {
128129
const resolvesEmpty = await nodeB.name.resolve(idB.id)
129130
expect(resolvesEmpty).to.be.eq(emptyDirCid)
130131

131-
try {
132-
await nodeA.name.resolve(idB.id)
133-
} catch (error) {
134-
expect(error).to.exist()
135-
}
132+
await expect(nodeA.name.resolve(idB.id))
133+
.to.eventually.be.rejected()
134+
.and.to.have.property('code', 'ERR_NO_RECORD_FOUND')
136135

137136
const publish = await nodeB.name.publish(path)
138137
expect(publish).to.be.eql({

0 commit comments

Comments
 (0)