refactor(integration): messaging tests with
MODIFY
packages/@integration/src/http/index.ts
packages/@integration/src/http/index.ts
-import { IO } from 'fp-ts/lib/IO';
+import * as T from 'fp-ts/lib/Task';
+import { pipe } from 'fp-ts/lib/pipeable';
import { createServer, httpListener } from '@marblejs/core';
import { isTestEnv, getPortEnv } from '@marblejs/core/dist/+internal/utils';
import { logger$ } from '@marblejs/middleware-logger';
@@ -17,12 +18,13 @@ export const listener = httpListener({
],
});
-export const server = createServer({
+export const server = () => createServer({
port: getPortEnv(),
listener,
});
-const main: IO<void> = async () =>
- !isTestEnv() && await (await server)();
+export const main = !isTestEnv()
+ ? pipe(server, T.map(run => run()))
+ : T.of(undefined);
main();

MODIFY
packages/@integration/src/messaging/client.ts
packages/@integration/src/messaging/client.ts
@@ -3,7 +3,8 @@ import { requestValidator$, t } from '@marblejs/middleware-io';
import { messagingClient, MessagingClient, Transport } from '@marblejs/messaging';
import { r, createServer, createContextToken, httpListener, use, useContext, bindEagerlyTo, combineRoutes, ContextToken } from '@marblejs/core';
import { isTestEnv, getPortEnv } from '@marblejs/core/dist/+internal/utils';
-import { IO } from 'fp-ts/lib/IO';
+import * as T from 'fp-ts/lib/Task';
+import { pipe } from 'fp-ts/lib/pipeable';
import { forkJoin } from 'rxjs';
import { map, mergeMap } from 'rxjs/operators';
@@ -64,19 +65,24 @@ const redis$ = combineRoutes('/redis', [
fib$(RedisClientToken),
]);
-export const server = createServer({
- port: getPortEnv(),
- listener: httpListener({
- middlewares: [logger$()],
- effects: [amqp$, redis$],
- }),
- dependencies: [
+export const dependencies = [
bindEagerlyTo(AmqpClientToken)(amqpClient),
bindEagerlyTo(RedisClientToken)(redisClient),
- ],
+];
+
+export const listener = httpListener({
+ middlewares: [logger$()],
+ effects: [amqp$, redis$],
+});
+
+export const server = () => createServer({
+ port: getPortEnv(),
+ listener,
+ dependencies,
});
-const main: IO<void> = async () =>
- !isTestEnv() && await (await server)();
+export const main = !isTestEnv()
+ ? pipe(server, T.map(run => run()))
+ : T.of(undefined);
main();

MODIFY
packages/@integration/test/http.integration.spec.ts
packages/@integration/test/http.integration.spec.ts
@@ -11,7 +11,7 @@ const useTestBedSetup = createTestBedSetup({
describe('API integration', () => {
const testBedSetup = useTestBedSetup()
- afterEach(() => testBedSetup.cleanup());
+ afterEach(testBedSetup.cleanup);
test('POST "/" returns 404 when route not found', async () => {
const testBed = await testBedSetup.useTestBed();

MODIFY
packages/@integration/test/messaging.integration.spec.ts
packages/@integration/test/messaging.integration.spec.ts
-/* eslint-disable @typescript-eslint/no-unused-vars */
-
-import * as request from 'supertest';
-import { createHttpServerTestBed } from '@marblejs/core/dist/+internal/testing';
+import { pipe } from 'fp-ts/lib/pipeable';
+import { createTestBedSetup, createHttpTestBed } from '@marblejs/testing';
import { createMicroserviceTestBed } from '@marblejs/messaging/dist/+internal/testing';
-import { server } from '../src/messaging/client';
+import { dependencies, listener } from '../src/messaging/client';
import { amqpMicroservice, redisMicroservice } from '../src/messaging/server';
+const useHttpTestBedSetup = createTestBedSetup({
+ dependencies,
+ testBed: createHttpTestBed({ listener }),
+});
+
describe('messaging integration', () => {
- const httpTestBed = createHttpServerTestBed(server);
+ const httpTestBedSetup = useHttpTestBedSetup();
- createMicroserviceTestBed(amqpMicroservice);
- createMicroserviceTestBed(redisMicroservice);
+ afterEach(httpTestBedSetup.cleanup);
- beforeAll(async () => {
- jest.spyOn(console, 'log').mockImplementation();
- jest.spyOn(console, 'info').mockImplementation();
- });
+ createMicroserviceTestBed(redisMicroservice);
+ createMicroserviceTestBed(amqpMicroservice);
test.each([
'redis',
'amqp',
- ])('GET /%s/fib returns 10, 11, 12, 13, 14 th fibonacci number', async type =>
- request(httpTestBed.getInstance())
- .get(`/${type}/fib/10`)
- .expect(200, [
+ ])('GET /%s/fib returns 10, 11, 12, 13, 14 th fibonacci number', async type => {
+ const testBed = await httpTestBedSetup.useTestBed();
+
+ const response = await pipe(
+ testBed.req('GET'),
+ testBed.withPath(`/${type}/fib/10`),
+ testBed.send,
+ );
+
+ expect(response.statusCode).toEqual(200);
+ expect(response.body).toEqual([
{ type: 'FIB_RESULT', payload: 55 },
{ type: 'FIB_RESULT', payload: 89 },
{ type: 'FIB_RESULT', payload: 144 },
{ type: 'FIB_RESULT', payload: 233 },
{ type: 'FIB_RESULT', payload: 377 },
- ]));
+ ]);
+ });
});

refactor(integration): messaging tests with @marblejs/testing