test(@integration): messaging integration tests
MODIFY
.travis.yml
.travis.yml
@@ -16,9 +16,11 @@ git:
install:
- npm i -g yarn
- npm i -g codecov
- - yarn
script:
+ - yarn
+
+after_success:
- codecov
branches:

MODIFY
package.json
package.json
"unlink:all": "lerna exec -- yarn unlink",
"test": "yarn test:unit && yarn test:integration",
"test:unit": "SCOPE=unit jest --expand --coverage --detectOpenHandles",
- "test:integration": "SCOPE=integration jest --expand --detectOpenHandles",
+ "test:integration": "SCOPE=integration ./scripts/run-integration-tests.sh",
"test:watch": "jest --expand --onlyChanged --watch",
"clean": "lerna run clean",
"purge": "yarn clean && rimraf node_modules",

ADD
packages/@integration/docker-compose.yml
+version: "3"
+
+services:
+ marble-rabbit:
+ container_name: "marble-rabbit"
+ hostname: rabbit
+ image: "rabbitmq:management"
+ ports:
+ - "15672:15672"
+ - "5672:5672"
+ tty: true

MODIFY
packages/@integration/src/messaging/client.ts
packages/@integration/src/messaging/client.ts
@@ -30,8 +30,8 @@ const fib$ = r.pipe(
r.useEffect((req$, _, { ask }) => req$.pipe(
mapTo(ask(ClientToken)),
mergeMap(client => forkJoin(
- client.map(c => c.send({ type: 'FIB', payload: 42 })).getOrElse(of({})),
- client.map(c => c.send({ type: 'FIB', payload: 42 })).getOrElse(of({})),
+ client.map(c => c.send({ type: 'FIB', payload: 10 })).getOrElse(of({})),
+ client.map(c => c.send({ type: 'FIB', payload: 10 })).getOrElse(of({})),
)),
map(body => ({ body })),
)),
@@ -58,4 +58,6 @@ export const server = createServer({
),
});
-server.run();
+server.run(
+ process.env.NODE_ENV !== 'test'
+);

MODIFY
packages/@integration/src/messaging/server.ts
packages/@integration/src/messaging/server.ts
-import { createContext, matchEvent, use } from '@marblejs/core';
+import { matchEvent, use, createContext } from '@marblejs/core';
import { eventValidator$, t } from '@marblejs/middleware-io';
import { messagingListener, Transport, MsgEffect, MsgMiddlewareEffect } from '@marblejs/messaging';
import { map, tap } from 'rxjs/operators';
@@ -21,7 +21,7 @@ const fibonacci$: MsgEffect = event$ =>
map(payload => ({ type: 'FIB_RESULT', payload })),
);
-messagingListener({
+const listener = messagingListener({
effects: [
fibonacci$,
],
@@ -34,4 +34,10 @@ messagingListener({
queue: 'test_queue',
queueOptions: { durable: false },
},
-})().run(createContext());
+});
+
+export const server = listener();
+
+if (process.env.NODE_ENV !== 'test') {
+ server.run(createContext());
+}

ADD
packages/@integration/test/messaging.integration.spec.ts
+import * as request from 'supertest';
+import { server as httpServer } from '../src/messaging/client';
+import { server as messagingServer } from '../src/messaging/server';
+import { createContext } from '@marblejs/core';
+
+const microservice = messagingServer.run(createContext());
+
+beforeAll(() => microservice);
+
+describe('messaging integration', () => {
+ test('GET /fib returns 10th fibonacci number', async () =>
+ request(httpServer.server)
+ .get('/fib')
+ .expect(200, [
+ { type: 'FIB_RESULT', payload: 55 },
+ { type: 'FIB_RESULT', payload: 55 },
+ ]));
+});
+
+afterAll(() => microservice.then(s => s.close()));

ADD
scripts/run-integration-tests.sh
+#!/bin/bash
+
+RABBITMQ_CONTAINER=marble-rabbit
+
+function wait_for_rabbit () {
+ until docker exec $RABBITMQ_CONTAINER rabbitmqctl cluster_status > /dev/null 2>&1
+ do
+ sleep 2
+ echo "Waiting for rabbitmq..."
+ done
+}
+
+docker-compose -f packages/@integration/docker-compose.yml up -d
+
+wait_for_rabbit
+
+jest --expand --detectOpenHandles --runInBand
+docker-compose -f packages/@integration/docker-compose.yml down

test(@integration): messaging integration tests