Skip to content

Commit 6fd7d0e

Browse files
committed
test: use chai as promised
1 parent 870d80d commit 6fd7d0e

File tree

7 files changed

+71
-131
lines changed

7 files changed

+71
-131
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"abortable-iterator": "^2.1.0",
8181
"aegir": "^20.0.0",
8282
"chai": "^4.2.0",
83+
"chai-as-promised": "^7.1.1",
8384
"chai-checkmark": "^1.0.1",
8485
"cids": "^0.7.1",
8586
"delay": "^4.3.0",

test/dialing/direct.node.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-as-promised'))
67
const { expect } = chai
78
const sinon = require('sinon')
89
const Transport = require('libp2p-tcp')
@@ -77,14 +78,9 @@ describe('Dialing (direct, TCP)', () => {
7778
it('should fail to connect to an unsupported multiaddr', async () => {
7879
const dialer = new Dialer({ transportManager: localTM })
7980

80-
try {
81-
await dialer.connectToMultiaddr(unsupportedAddr)
82-
} catch (err) {
83-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
84-
return
85-
}
86-
87-
expect.fail('Dial should have failed')
81+
await expect(dialer.connectToMultiaddr(unsupportedAddr))
82+
.to.eventually.be.rejected()
83+
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
8884
})
8985

9086
it('should be able to connect to a given peer info', async () => {
@@ -121,14 +117,9 @@ describe('Dialing (direct, TCP)', () => {
121117
const peerInfo = new PeerInfo(peerId)
122118
peerInfo.multiaddrs.add(unsupportedAddr)
123119

124-
try {
125-
await dialer.connectToPeer(peerInfo)
126-
} catch (err) {
127-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED)
128-
return
129-
}
130-
131-
expect.fail('Dial should have failed')
120+
await expect(dialer.connectToPeer(peerInfo))
121+
.to.eventually.be.rejected()
122+
.and.to.have.property('code', ErrorCodes.ERR_CONNECTION_FAILED)
132123
})
133124

134125
it('should abort dials on queue task timeout', async () => {
@@ -144,14 +135,9 @@ describe('Dialing (direct, TCP)', () => {
144135
expect(options.signal.aborted).to.equal(true)
145136
})
146137

147-
try {
148-
await dialer.connectToMultiaddr(remoteAddr)
149-
} catch (err) {
150-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT)
151-
return
152-
}
153-
154-
expect.fail('Dial should have failed')
138+
await expect(dialer.connectToMultiaddr(remoteAddr))
139+
.to.eventually.be.rejected()
140+
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
155141
})
156142

157143
it('should dial to the max concurrency', async () => {

test/dialing/direct.spec.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-as-promised'))
67
const { expect } = chai
78
const sinon = require('sinon')
89
const pDefer = require('p-defer')
@@ -67,14 +68,9 @@ describe('Dialing (direct, WebSockets)', () => {
6768
it('should fail to connect to an unsupported multiaddr', async () => {
6869
const dialer = new Dialer({ transportManager: localTM })
6970

70-
try {
71-
await dialer.connectToMultiaddr(unsupportedAddr)
72-
} catch (err) {
73-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_DIAL_FAILED)
74-
return
75-
}
76-
77-
expect.fail('Dial should have failed')
71+
await expect(dialer.connectToMultiaddr(unsupportedAddr))
72+
.to.eventually.be.rejected()
73+
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_DIAL_FAILED)
7874
})
7975

8076
it('should be able to connect to a given peer', async () => {
@@ -94,14 +90,9 @@ describe('Dialing (direct, WebSockets)', () => {
9490
const peerInfo = new PeerInfo(peerId)
9591
peerInfo.multiaddrs.add(unsupportedAddr)
9692

97-
try {
98-
await dialer.connectToPeer(peerInfo)
99-
} catch (err) {
100-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED)
101-
return
102-
}
103-
104-
expect.fail('Dial should have failed')
93+
await expect(dialer.connectToPeer(peerInfo))
94+
.to.eventually.be.rejected()
95+
.and.to.have.property('code', ErrorCodes.ERR_CONNECTION_FAILED)
10596
})
10697

10798
it('should abort dials on queue task timeout', async () => {
@@ -117,14 +108,9 @@ describe('Dialing (direct, WebSockets)', () => {
117108
expect(options.signal.aborted).to.equal(true)
118109
})
119110

120-
try {
121-
await dialer.connectToMultiaddr(remoteAddr)
122-
} catch (err) {
123-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT)
124-
return
125-
}
126-
127-
expect.fail('Dial should have failed')
111+
await expect(dialer.connectToMultiaddr(remoteAddr))
112+
.to.eventually.be.rejected()
113+
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
128114
})
129115

130116
it('should dial to the max concurrency', async () => {

test/dialing/relay.node.js

+13-29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-as-promised'))
67
const { expect } = chai
78
const sinon = require('sinon')
89

@@ -87,13 +88,9 @@ describe('Dialing (via relay, TCP)', () => {
8788
.encapsulate(`/p2p/${relayIdString}`)
8889
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
8990

90-
try {
91-
await srcLibp2p.dial(dialAddr)
92-
expect.fail('Dial should have failed')
93-
} catch (err) {
94-
expect(err).to.exist()
95-
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
96-
}
91+
await expect(srcLibp2p.dial(dialAddr))
92+
.to.eventually.be.rejected()
93+
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
9794
})
9895

9996
it('should not stay connected to a relay when not already connected and HOP fails', async () => {
@@ -104,13 +101,9 @@ describe('Dialing (via relay, TCP)', () => {
104101
.encapsulate(`/p2p/${relayIdString}`)
105102
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
106103

107-
try {
108-
await srcLibp2p.dial(dialAddr)
109-
expect.fail('Dial should have failed')
110-
} catch (err) {
111-
expect(err).to.exist()
112-
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
113-
}
104+
await expect(srcLibp2p.dial(dialAddr))
105+
.to.eventually.be.rejected()
106+
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
114107

115108
// We should not be connected to the relay, because we weren't before the dial
116109
const srcToRelayConn = srcLibp2p.registrar.getConnection(relayLibp2p.peerInfo)
@@ -125,16 +118,11 @@ describe('Dialing (via relay, TCP)', () => {
125118
.encapsulate(`/p2p/${relayIdString}`)
126119
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)
127120

128-
// Connect to the relay first
129121
await srcLibp2p.dial(relayAddr)
130122

131-
try {
132-
await srcLibp2p.dial(dialAddr)
133-
expect.fail('Dial should have failed')
134-
} catch (err) {
135-
expect(err).to.exist()
136-
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
137-
}
123+
await expect(srcLibp2p.dial(dialAddr))
124+
.to.eventually.be.rejected()
125+
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
138126

139127
const srcToRelayConn = srcLibp2p.registrar.getConnection(relayLibp2p.peerInfo)
140128
expect(srcToRelayConn).to.exist()
@@ -159,13 +147,9 @@ describe('Dialing (via relay, TCP)', () => {
159147
buffer: Buffer.from('an invalid multiaddr')
160148
}])
161149

162-
try {
163-
await srcLibp2p.dial(dialAddr)
164-
expect.fail('Dial should have failed')
165-
} catch (err) {
166-
expect(err).to.exist()
167-
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
168-
}
150+
await expect(srcLibp2p.dial(dialAddr))
151+
.to.eventually.be.rejected()
152+
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
169153

170154
const dstToRelayConn = dstLibp2p.registrar.getConnection(relayLibp2p.peerInfo)
171155
expect(dstToRelayConn).to.exist()

test/identify/index.spec.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-as-promised'))
67
const { expect } = chai
78
const sinon = require('sinon')
89

@@ -98,20 +99,18 @@ describe('Identify', () => {
9899
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })
99100

100101
// Run identify
101-
try {
102-
await Promise.all([
103-
localIdentify.identify(localConnectionMock, localPeer.id),
104-
remoteIdentify.handleMessage({
105-
connection: remoteConnectionMock,
106-
stream: remote,
107-
protocol: multicodecs.IDENTIFY
108-
})
109-
])
110-
expect.fail('should have thrown')
111-
} catch (err) {
112-
expect(err).to.exist()
113-
expect(err.code).to.eql(Errors.ERR_INVALID_PEER)
114-
}
102+
const identifyPromise = Promise.all([
103+
localIdentify.identify(localConnectionMock, localPeer.id),
104+
remoteIdentify.handleMessage({
105+
connection: remoteConnectionMock,
106+
stream: remote,
107+
protocol: multicodecs.IDENTIFY
108+
})
109+
])
110+
111+
await expect(identifyPromise)
112+
.to.eventually.be.rejected()
113+
.and.to.have.property('code', Errors.ERR_INVALID_PEER)
115114
})
116115

117116
describe('push', () => {

test/registrar/registrar.spec.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,14 @@ describe('registrar', () => {
2424
})
2525

2626
it('should fail to register a protocol if no multicodec is provided', () => {
27-
try {
28-
registrar.register()
29-
} catch (err) {
30-
expect(err).to.exist()
31-
return
32-
}
33-
throw new Error('should fail to register a protocol if no multicodec is provided')
27+
expect(() => registrar.register()).to.throw()
3428
})
3529

3630
it('should fail to register a protocol if an invalid topology is provided', () => {
3731
const fakeTopology = {
3832
random: 1
3933
}
40-
try {
41-
registrar.register()
42-
} catch (err) {
43-
expect(err).to.exist(fakeTopology)
44-
return
45-
}
46-
throw new Error('should fail to register a protocol if an invalid topology is provided')
34+
expect(() => registrar.register(fakeTopology)).to.throw()
4735
})
4836
})
4937

test/transports/transport-manager.spec.js

+22-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
const chai = require('chai')
55
chai.use(require('dirty-chai'))
6+
chai.use(require('chai-as-promised'))
67
const { expect } = chai
78
const sinon = require('sinon')
89

@@ -39,21 +40,25 @@ describe('Transport Manager (WebSockets)', () => {
3940
await tm.remove(Transport.prototype[Symbol.toStringTag])
4041
})
4142

42-
it('should not be able to add a transport without a key', () => {
43-
expect(() => {
43+
it('should not be able to add a transport without a key', async () => {
44+
// Chai as promised conflicts with normal `throws` validation,
45+
// so wrap the call in an async function
46+
await expect((async () => { // eslint-disable-line
4447
tm.add(undefined, Transport)
45-
}).to.throw().that.satisfies((err) => {
46-
return err.code === ErrorCodes.ERR_INVALID_KEY
47-
})
48+
})())
49+
.to.eventually.be.rejected()
50+
.and.to.have.property('code', ErrorCodes.ERR_INVALID_KEY)
4851
})
4952

50-
it('should not be able to add a transport twice', () => {
53+
it('should not be able to add a transport twice', async () => {
5154
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
52-
expect(() => {
55+
// Chai as promised conflicts with normal `throws` validation,
56+
// so wrap the call in an async function
57+
await expect((async () => { // eslint-disable-line
5358
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
54-
}).to.throw().that.satisfies((err) => {
55-
return err.code === ErrorCodes.ERR_DUPLICATE_TRANSPORT
56-
})
59+
})())
60+
.to.eventually.be.rejected()
61+
.and.to.have.property('code', ErrorCodes.ERR_DUPLICATE_TRANSPORT)
5762
})
5863

5964
it('should be able to dial', async () => {
@@ -67,27 +72,18 @@ describe('Transport Manager (WebSockets)', () => {
6772
it('should fail to dial an unsupported address', async () => {
6873
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
6974
const addr = multiaddr('/ip4/127.0.0.1/tcp/0')
70-
try {
71-
await tm.dial(addr)
72-
} catch (err) {
73-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
74-
return
75-
}
76-
77-
expect.fail('Dial attempt should have failed')
75+
await expect(tm.dial(addr))
76+
.to.eventually.be.rejected()
77+
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
7878
})
7979

8080
it('should fail to listen with no valid address', async () => {
8181
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
8282
const addrs = [multiaddr('/ip4/127.0.0.1/tcp/0')]
83-
try {
84-
await tm.listen(addrs)
85-
} catch (err) {
86-
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_NO_VALID_ADDRESSES)
87-
return
88-
}
89-
90-
expect.fail('should have failed')
83+
84+
await expect(tm.listen(addrs))
85+
.to.eventually.be.rejected()
86+
.and.to.have.property('code', ErrorCodes.ERR_NO_VALID_ADDRESSES)
9187
})
9288
})
9389

0 commit comments

Comments
 (0)