diff --git a/lib/swagger.js b/lib/swagger.js index 6d35c4f94..c9d12d136 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -34,7 +34,7 @@ exports.run = function({ args, opts }) { function error(err) { console.log(err.error); - console.error('There was an error uploading!'.red); + return Promise.reject(new Error('There was an error uploading!')); } const options = { diff --git a/test/swagger.test.js b/test/swagger.test.js index 616834856..98a26ce2d 100644 --- a/test/swagger.test.js +++ b/test/swagger.test.js @@ -1,6 +1,5 @@ const nock = require('nock'); const config = require('config'); -const assert = require('assert'); const swagger = require('../cli').bind(null, 'swagger'); @@ -11,14 +10,26 @@ describe('swagger command', () => { afterAll(() => nock.cleanAll()); it('should error if no api key provided', () => - swagger(['./test/fixtures/swagger.json'], {}).catch(err => { - assert.equal(err.message, 'No api key provided. Please use --key'); - })); + expect(swagger(['./test/fixtures/swagger.json'], {})).rejects.toThrow( + 'No api key provided. Please use --key', + )); it('should error if no file provided', () => - swagger([], { key }).catch(err => { - assert.equal(err.message, 'No swagger file provided. Usage `rdme swagger `'); - })); + expect(swagger([], { key })).rejects.toThrow( + 'No swagger file provided. Usage `rdme swagger `', + )); + + it('should error if API errors', async () => { + const mock = nock(config.host) + .post('/api/v1/swagger', body => body.match('form-data; name="swagger"')) + .basicAuth({ user: key }) + .reply(400); + + await expect(swagger(['./test/fixtures/swagger.json'], { key })).rejects.toThrow( + 'There was an error uploading!', + ); + mock.done(); + }); it('should POST to the swagger api if no id provided', () => { const mock = nock(config.host)