From 6fd7d0e08f85040fbcdde322fc2249936d6e3fdd Mon Sep 17 00:00:00 2001 From: Jacob Heun Date: Fri, 29 Nov 2019 12:53:11 +0100 Subject: [PATCH] test: use chai as promised --- package.json | 1 + test/dialing/direct.node.js | 34 +++++----------- test/dialing/direct.spec.js | 34 +++++----------- test/dialing/relay.node.js | 42 ++++++-------------- test/identify/index.spec.js | 27 ++++++------- test/registrar/registrar.spec.js | 16 +------- test/transports/transport-manager.spec.js | 48 +++++++++++------------ 7 files changed, 71 insertions(+), 131 deletions(-) diff --git a/package.json b/package.json index 1fd0d25307..d459f30fb9 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "abortable-iterator": "^2.1.0", "aegir": "^20.0.0", "chai": "^4.2.0", + "chai-as-promised": "^7.1.1", "chai-checkmark": "^1.0.1", "cids": "^0.7.1", "delay": "^4.3.0", diff --git a/test/dialing/direct.node.js b/test/dialing/direct.node.js index 04d63b40e4..8fbe5d7944 100644 --- a/test/dialing/direct.node.js +++ b/test/dialing/direct.node.js @@ -3,6 +3,7 @@ const chai = require('chai') chai.use(require('dirty-chai')) +chai.use(require('chai-as-promised')) const { expect } = chai const sinon = require('sinon') const Transport = require('libp2p-tcp') @@ -77,14 +78,9 @@ describe('Dialing (direct, TCP)', () => { it('should fail to connect to an unsupported multiaddr', async () => { const dialer = new Dialer({ transportManager: localTM }) - try { - await dialer.connectToMultiaddr(unsupportedAddr) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE) - return - } - - expect.fail('Dial should have failed') + await expect(dialer.connectToMultiaddr(unsupportedAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE) }) it('should be able to connect to a given peer info', async () => { @@ -121,14 +117,9 @@ describe('Dialing (direct, TCP)', () => { const peerInfo = new PeerInfo(peerId) peerInfo.multiaddrs.add(unsupportedAddr) - try { - await dialer.connectToPeer(peerInfo) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED) - return - } - - expect.fail('Dial should have failed') + await expect(dialer.connectToPeer(peerInfo)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_CONNECTION_FAILED) }) it('should abort dials on queue task timeout', async () => { @@ -144,14 +135,9 @@ describe('Dialing (direct, TCP)', () => { expect(options.signal.aborted).to.equal(true) }) - try { - await dialer.connectToMultiaddr(remoteAddr) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT) - return - } - - expect.fail('Dial should have failed') + await expect(dialer.connectToMultiaddr(remoteAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_TIMEOUT) }) it('should dial to the max concurrency', async () => { diff --git a/test/dialing/direct.spec.js b/test/dialing/direct.spec.js index eb1b158b5e..f5eaff9aa8 100644 --- a/test/dialing/direct.spec.js +++ b/test/dialing/direct.spec.js @@ -3,6 +3,7 @@ const chai = require('chai') chai.use(require('dirty-chai')) +chai.use(require('chai-as-promised')) const { expect } = chai const sinon = require('sinon') const pDefer = require('p-defer') @@ -67,14 +68,9 @@ describe('Dialing (direct, WebSockets)', () => { it('should fail to connect to an unsupported multiaddr', async () => { const dialer = new Dialer({ transportManager: localTM }) - try { - await dialer.connectToMultiaddr(unsupportedAddr) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_DIAL_FAILED) - return - } - - expect.fail('Dial should have failed') + await expect(dialer.connectToMultiaddr(unsupportedAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_DIAL_FAILED) }) it('should be able to connect to a given peer', async () => { @@ -94,14 +90,9 @@ describe('Dialing (direct, WebSockets)', () => { const peerInfo = new PeerInfo(peerId) peerInfo.multiaddrs.add(unsupportedAddr) - try { - await dialer.connectToPeer(peerInfo) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED) - return - } - - expect.fail('Dial should have failed') + await expect(dialer.connectToPeer(peerInfo)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_CONNECTION_FAILED) }) it('should abort dials on queue task timeout', async () => { @@ -117,14 +108,9 @@ describe('Dialing (direct, WebSockets)', () => { expect(options.signal.aborted).to.equal(true) }) - try { - await dialer.connectToMultiaddr(remoteAddr) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT) - return - } - - expect.fail('Dial should have failed') + await expect(dialer.connectToMultiaddr(remoteAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_TIMEOUT) }) it('should dial to the max concurrency', async () => { diff --git a/test/dialing/relay.node.js b/test/dialing/relay.node.js index c71b539a9f..976b23ce71 100644 --- a/test/dialing/relay.node.js +++ b/test/dialing/relay.node.js @@ -3,6 +3,7 @@ const chai = require('chai') chai.use(require('dirty-chai')) +chai.use(require('chai-as-promised')) const { expect } = chai const sinon = require('sinon') @@ -87,13 +88,9 @@ describe('Dialing (via relay, TCP)', () => { .encapsulate(`/p2p/${relayIdString}`) .encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`) - try { - await srcLibp2p.dial(dialAddr) - expect.fail('Dial should have failed') - } catch (err) { - expect(err).to.exist() - expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) - } + await expect(srcLibp2p.dial(dialAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) }) 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)', () => { .encapsulate(`/p2p/${relayIdString}`) .encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`) - try { - await srcLibp2p.dial(dialAddr) - expect.fail('Dial should have failed') - } catch (err) { - expect(err).to.exist() - expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) - } + await expect(srcLibp2p.dial(dialAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) // We should not be connected to the relay, because we weren't before the dial const srcToRelayConn = srcLibp2p.registrar.getConnection(relayLibp2p.peerInfo) @@ -125,16 +118,11 @@ describe('Dialing (via relay, TCP)', () => { .encapsulate(`/p2p/${relayIdString}`) .encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`) - // Connect to the relay first await srcLibp2p.dial(relayAddr) - try { - await srcLibp2p.dial(dialAddr) - expect.fail('Dial should have failed') - } catch (err) { - expect(err).to.exist() - expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) - } + await expect(srcLibp2p.dial(dialAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) const srcToRelayConn = srcLibp2p.registrar.getConnection(relayLibp2p.peerInfo) expect(srcToRelayConn).to.exist() @@ -159,13 +147,9 @@ describe('Dialing (via relay, TCP)', () => { buffer: Buffer.from('an invalid multiaddr') }]) - try { - await srcLibp2p.dial(dialAddr) - expect.fail('Dial should have failed') - } catch (err) { - expect(err).to.exist() - expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) - } + await expect(srcLibp2p.dial(dialAddr)) + .to.eventually.be.rejected() + .and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED) const dstToRelayConn = dstLibp2p.registrar.getConnection(relayLibp2p.peerInfo) expect(dstToRelayConn).to.exist() diff --git a/test/identify/index.spec.js b/test/identify/index.spec.js index 2dd4e5a66b..8cc372c60d 100644 --- a/test/identify/index.spec.js +++ b/test/identify/index.spec.js @@ -3,6 +3,7 @@ const chai = require('chai') chai.use(require('dirty-chai')) +chai.use(require('chai-as-promised')) const { expect } = chai const sinon = require('sinon') @@ -98,20 +99,18 @@ describe('Identify', () => { sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY }) // Run identify - try { - await Promise.all([ - localIdentify.identify(localConnectionMock, localPeer.id), - remoteIdentify.handleMessage({ - connection: remoteConnectionMock, - stream: remote, - protocol: multicodecs.IDENTIFY - }) - ]) - expect.fail('should have thrown') - } catch (err) { - expect(err).to.exist() - expect(err.code).to.eql(Errors.ERR_INVALID_PEER) - } + const identifyPromise = Promise.all([ + localIdentify.identify(localConnectionMock, localPeer.id), + remoteIdentify.handleMessage({ + connection: remoteConnectionMock, + stream: remote, + protocol: multicodecs.IDENTIFY + }) + ]) + + await expect(identifyPromise) + .to.eventually.be.rejected() + .and.to.have.property('code', Errors.ERR_INVALID_PEER) }) describe('push', () => { diff --git a/test/registrar/registrar.spec.js b/test/registrar/registrar.spec.js index 9114e03590..a56854d42b 100644 --- a/test/registrar/registrar.spec.js +++ b/test/registrar/registrar.spec.js @@ -24,26 +24,14 @@ describe('registrar', () => { }) it('should fail to register a protocol if no multicodec is provided', () => { - try { - registrar.register() - } catch (err) { - expect(err).to.exist() - return - } - throw new Error('should fail to register a protocol if no multicodec is provided') + expect(() => registrar.register()).to.throw() }) it('should fail to register a protocol if an invalid topology is provided', () => { const fakeTopology = { random: 1 } - try { - registrar.register() - } catch (err) { - expect(err).to.exist(fakeTopology) - return - } - throw new Error('should fail to register a protocol if an invalid topology is provided') + expect(() => registrar.register(fakeTopology)).to.throw() }) }) diff --git a/test/transports/transport-manager.spec.js b/test/transports/transport-manager.spec.js index d40e82245a..ab08fc1759 100644 --- a/test/transports/transport-manager.spec.js +++ b/test/transports/transport-manager.spec.js @@ -3,6 +3,7 @@ const chai = require('chai') chai.use(require('dirty-chai')) +chai.use(require('chai-as-promised')) const { expect } = chai const sinon = require('sinon') @@ -39,21 +40,25 @@ describe('Transport Manager (WebSockets)', () => { await tm.remove(Transport.prototype[Symbol.toStringTag]) }) - it('should not be able to add a transport without a key', () => { - expect(() => { + it('should not be able to add a transport without a key', async () => { + // Chai as promised conflicts with normal `throws` validation, + // so wrap the call in an async function + await expect((async () => { // eslint-disable-line tm.add(undefined, Transport) - }).to.throw().that.satisfies((err) => { - return err.code === ErrorCodes.ERR_INVALID_KEY - }) + })()) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_INVALID_KEY) }) - it('should not be able to add a transport twice', () => { + it('should not be able to add a transport twice', async () => { tm.add(Transport.prototype[Symbol.toStringTag], Transport) - expect(() => { + // Chai as promised conflicts with normal `throws` validation, + // so wrap the call in an async function + await expect((async () => { // eslint-disable-line tm.add(Transport.prototype[Symbol.toStringTag], Transport) - }).to.throw().that.satisfies((err) => { - return err.code === ErrorCodes.ERR_DUPLICATE_TRANSPORT - }) + })()) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_DUPLICATE_TRANSPORT) }) it('should be able to dial', async () => { @@ -67,27 +72,18 @@ describe('Transport Manager (WebSockets)', () => { it('should fail to dial an unsupported address', async () => { tm.add(Transport.prototype[Symbol.toStringTag], Transport) const addr = multiaddr('/ip4/127.0.0.1/tcp/0') - try { - await tm.dial(addr) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE) - return - } - - expect.fail('Dial attempt should have failed') + await expect(tm.dial(addr)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE) }) it('should fail to listen with no valid address', async () => { tm.add(Transport.prototype[Symbol.toStringTag], Transport) const addrs = [multiaddr('/ip4/127.0.0.1/tcp/0')] - try { - await tm.listen(addrs) - } catch (err) { - expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_NO_VALID_ADDRESSES) - return - } - - expect.fail('should have failed') + + await expect(tm.listen(addrs)) + .to.eventually.be.rejected() + .and.to.have.property('code', ErrorCodes.ERR_NO_VALID_ADDRESSES) }) })