diff --git a/__tests__/unit/index.js b/__tests__/unit/index.js index 3bd4973..dbfe6a0 100644 --- a/__tests__/unit/index.js +++ b/__tests__/unit/index.js @@ -96,13 +96,13 @@ describe('knativebus', () => { expect(axios).not.toBeCalled() }) - it('throws an error if aggregate has no config', () => { + it('throws an error if aggregate has no config', async () => { const bus = knativebus(testBusConfig) const command = 'turtle.crawl' const data = { id: 1 } try { - bus.send(command, data) + await bus.send(command, data) } catch (err) { expect(err).toEqual(new Error('"turtle" has not been configured')) } @@ -170,26 +170,26 @@ describe('knativebus', () => { const data = { id: 1 } try { - bus.publish(event, data) + await bus.publish(event, data) } catch (e) { expect(e).toEqual(new Error('"example" events broker url has not been configured')) } expect(axios).not.toBeCalled() }) - it('throws an error if aggregate has no config', () => { + it('throws an error if aggregate has no config', async () => { const bus = knativebus(testBusConfig) const event = 'turtle.crawled' const data = { id: 1 } try { - bus.publish(event, data) + await bus.publish(event, data) } catch (err) { expect(err).toEqual(new Error('"turtle" has not been configured')) } }) - it('publish', () => { + it('publish', async () => { const bus = knativebus(testBusConfig) const axios = require('axios') @@ -197,7 +197,7 @@ describe('knativebus', () => { const event = 'example.event-happened' const data = { id: 1 } - bus.publish(event, data) + await bus.publish(event, data) expect(axios).toBeCalledWith({ method: 'post', url: testBusConfig.aggregates.example.events, @@ -206,7 +206,7 @@ describe('knativebus', () => { }, { timeout: 0 }) }) - it('publish with retry', () => { + it('publish with retry', async () => { const bus = knativebus({ ...testBusConfig, retry: true @@ -217,7 +217,7 @@ describe('knativebus', () => { const event = 'example.event-happened' const data = { id: 1 } - bus.publish(event, data) + await bus.publish(event, data) expect(axios).toBeCalledWith({ method: 'post', url: testBusConfig.aggregates.example.events, diff --git a/src/index.js b/src/index.js index f3e87a4..7a7c9a5 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,7 @@ export const knativebus = (config) => { } return { - publish: (type, data) => { + publish: async (type, data) => { const splitType = type.split('.') if (splitType.length < 2) throw new Error('format events as {aggregate}.{event-happened}') @@ -48,7 +48,7 @@ export const knativebus = (config) => { info(`Publishing CloudEvent to KNative domain events channel (${modelDomainEventsChannel}): ${JSON.stringify(ce, null, 2)}`) - return axios({ + const result = await axios({ method: 'post', url: modelDomainEventsChannel, data: message.body, @@ -56,8 +56,10 @@ export const knativebus = (config) => { }, { timeout }) + + return result }, - send: (type, data) => { + send: async (type, data) => { const splitType = type.split('.') if (splitType.length < 2) throw new Error('format commands as {aggregate}.{command}') @@ -83,12 +85,14 @@ export const knativebus = (config) => { info(`Sending cloud event to KNative model broker (${modelBroker}): ${JSON.stringify(ce, null, 2)}`) - return axios({ + const result = await axios({ method: 'post', url: modelBroker, data: message.body, headers: message.headers }, { timeout }) + + return result } } }