Skip to content

Commit

Permalink
Added easier possibility to check both api response and events in API…
Browse files Browse the repository at this point in the history
… integrationt ests
  • Loading branch information
oskardudycz committed Feb 29, 2024
1 parent 3f63eed commit 6b75b15
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
8 changes: 5 additions & 3 deletions docs/snippets/gettingStarted/webApi/apiBDD.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
existingStream,
expectError,
expectNewEvents,
expectResponse,
getApplication,
} from '@event-driven-io/emmett-expressjs';
import { beforeEach, describe, it } from 'node:test';
Expand All @@ -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()
Expand Down Expand Up @@ -67,6 +71,7 @@ describe('ShoppingCart', () => {
request.post(`/clients/${clientId}/shopping-carts/current/confirm`),
)
.then([
expectResponse(204),
expectNewEvents(shoppingCartId, [
{
type: 'ShoppingCartConfirmed',
Expand Down Expand Up @@ -114,9 +119,6 @@ describe('ShoppingCart', () => {
});
});

let clientId: string;
let shoppingCartId: string;

const getRandomProduct = (): PricedProductItem => {
return {
productId: uuid(),
Expand Down
28 changes: 15 additions & 13 deletions packages/emmett-expressjs/src/testing/apiSpecification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ export const existingStream = <EventType extends Event = Event>(
/////////// Asserts
////////////////////////////////

export type ResponseAssert = (response: Response) => boolean | void;

export type ApiSpecificationAssert<EventType extends Event = Event> =
| TestEventStream<EventType>[]
| ((response: Response) => boolean | void)
| {
events: TestEventStream<EventType>[];
responseMatches: (response: Response) => boolean;
};
| ResponseAssert
| [ResponseAssert, ...TestEventStream<EventType>[]];

export const expect = <EventType extends Event = Event>(
streamId: string,
Expand Down Expand Up @@ -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;
}
},
};
Expand Down

0 comments on commit 6b75b15

Please sign in to comment.