From 6b75b1555d452b3d634bad1b0c0f3ae91eebd763 Mon Sep 17 00:00:00 2001 From: Oskar Dudycz Date: Thu, 29 Feb 2024 09:32:33 +0100 Subject: [PATCH] Added easier possibility to check both api response and events in API integrationt ests --- .../gettingStarted/webApi/apiBDD.int.spec.ts | 8 ++++-- .../src/testing/apiSpecification.ts | 28 ++++++++++--------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/docs/snippets/gettingStarted/webApi/apiBDD.int.spec.ts b/docs/snippets/gettingStarted/webApi/apiBDD.int.spec.ts index aab9cc31..02e42c8a 100644 --- a/docs/snippets/gettingStarted/webApi/apiBDD.int.spec.ts +++ b/docs/snippets/gettingStarted/webApi/apiBDD.int.spec.ts @@ -10,6 +10,7 @@ import { existingStream, expectError, expectNewEvents, + expectResponse, getApplication, } from '@event-driven-io/emmett-expressjs'; import { beforeEach, describe, it } from 'node:test'; @@ -22,10 +23,13 @@ const getUnitPrice = (_productId: string) => { }; describe('ShoppingCart', () => { + let clientId: string; + let shoppingCartId: string; beforeEach(() => { clientId = uuid(); shoppingCartId = `shopping_cart:${clientId}:current`; }); + describe('When empty', () => { it('should add product item', () => { return given() @@ -67,6 +71,7 @@ describe('ShoppingCart', () => { request.post(`/clients/${clientId}/shopping-carts/current/confirm`), ) .then([ + expectResponse(204), expectNewEvents(shoppingCartId, [ { type: 'ShoppingCartConfirmed', @@ -114,9 +119,6 @@ describe('ShoppingCart', () => { }); }); - let clientId: string; - let shoppingCartId: string; - const getRandomProduct = (): PricedProductItem => { return { productId: uuid(), diff --git a/packages/emmett-expressjs/src/testing/apiSpecification.ts b/packages/emmett-expressjs/src/testing/apiSpecification.ts index 2930102d..5caf7961 100644 --- a/packages/emmett-expressjs/src/testing/apiSpecification.ts +++ b/packages/emmett-expressjs/src/testing/apiSpecification.ts @@ -37,13 +37,12 @@ export const existingStream = ( /////////// Asserts //////////////////////////////// +export type ResponseAssert = (response: Response) => boolean | void; + export type ApiSpecificationAssert = | TestEventStream[] - | ((response: Response) => boolean | void) - | { - events: TestEventStream[]; - responseMatches: (response: Response) => boolean; - }; + | ResponseAssert + | [ResponseAssert, ...TestEventStream[]]; export const expect = ( streamId: string, @@ -128,17 +127,20 @@ export const ApiSpecification = { if (succeded === false) assert.fail(); } else if (Array.isArray(verify)) { + const [first, ...rest] = verify; + + if (typeof first === 'function') { + const succeded = first(response); + + if (succeded === false) assert.fail(); + } + + const events = typeof first === 'function' ? rest : verify; + assertMatches( Array.from(eventStore.appendedEvents.values()), - verify, - ); - } else { - assert.ok(verify.responseMatches(response)); - assertMatches( - Array.from(eventStore.appendedEvents.values()), - givenStreams, + events, ); - return; } }, };