Skip to content

Commit

Permalink
Updated API tests to use strongly-typed Event Store type
Browse files Browse the repository at this point in the history
Previously it was always taking the Event Store implementation which was forcing to do casting while setting up, which was not great.

Fixed also Event Store wrapper to just override the needed methods passing the rest as they are. Without it, event store extensions (like MongoDB projections api) wouldn't work.

Removed event store wrapper usage from E2E tests, as it was redundant: checks are only made on responses.
  • Loading branch information
oskardudycz committed Dec 29, 2024
1 parent cd245e9 commit 6a18731
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 62 deletions.
5 changes: 2 additions & 3 deletions src/docs/snippets/gettingStarted/webApi/apiBDD.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ShoppingCartStatus } from './shoppingCart';
import { shoppingCartApi } from './simpleApi';

// #region getting-started-e2e-tests
import { type EventStore } from '@event-driven-io/emmett';
import { getEventStoreDBEventStore } from '@event-driven-io/emmett-esdb';
import {
ApiE2ESpecification,
Expand All @@ -29,8 +28,8 @@ void describe('ShoppingCart E2E', () => {
esdbContainer = await new EventStoreDBContainer().start();

given = ApiE2ESpecification.for(
(): EventStore => getEventStoreDBEventStore(esdbContainer.getClient()),
(eventStore: EventStore) =>
() => getEventStoreDBEventStore(esdbContainer.getClient()),
(eventStore) =>
getApplication({
apis: [
shoppingCartApi(
Expand Down
9 changes: 3 additions & 6 deletions src/docs/snippets/gettingStarted/webApi/apiBDD.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import { beforeEach, describe, it } from 'node:test';
import type { PricedProductItem, ShoppingCartEvent } from '../events';
import { shoppingCartApi } from './simpleApi';
// #region getting-started-integration-tests
import {
getInMemoryEventStore,
type EventStore,
} from '@event-driven-io/emmett';
import { getInMemoryEventStore } from '@event-driven-io/emmett';
import {
ApiSpecification,
existingStream,
Expand Down Expand Up @@ -119,8 +116,8 @@ void describe('ShoppingCart', () => {
const unitPrice = Math.random() * 10;

const given = ApiSpecification.for<ShoppingCartEvent>(
(): EventStore => getInMemoryEventStore(),
(eventStore: EventStore) =>
() => getInMemoryEventStore(),
(eventStore) =>
getApplication({
apis: [
shoppingCartApi(
Expand Down
5 changes: 2 additions & 3 deletions src/docs/snippets/gettingStarted/webApi/apiBDDE2EGiven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,15 @@ void describe('ShoppingCart E2E', () => {
// #endregion test-container

// #region given
import { type EventStore } from '@event-driven-io/emmett';
import { getEventStoreDBEventStore } from '@event-driven-io/emmett-esdb';
import {
ApiE2ESpecification,
getApplication,
} from '@event-driven-io/emmett-expressjs';

const given = ApiE2ESpecification.for(
(): EventStore => getEventStoreDBEventStore(esdbContainer.getClient()),
(eventStore: EventStore) =>
() => getEventStoreDBEventStore(esdbContainer.getClient()),
(eventStore) =>
getApplication({
apis: [
shoppingCartApi(
Expand Down
5 changes: 2 additions & 3 deletions src/docs/snippets/gettingStarted/webApi/apiBDDE2ETest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { type EventStore } from '@event-driven-io/emmett';
import {
ApiE2ESpecification,
getApplication,
Expand All @@ -15,8 +14,8 @@ const now = new Date();
const unitPrice = Math.random() * 10;

const given = ApiE2ESpecification.for(
(): EventStore => getEventStoreDBEventStore(esdbContainer.getClient()),
(eventStore: EventStore) =>
() => getEventStoreDBEventStore(esdbContainer.getClient()),
(eventStore) =>
getApplication({
apis: [
shoppingCartApi(
Expand Down
9 changes: 3 additions & 6 deletions src/docs/snippets/gettingStarted/webApi/apiBDDIntGiven.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import type { ShoppingCartEvent } from '../events';
import { shoppingCartApi } from './simpleApi';

// #region given
import {
getInMemoryEventStore,
type EventStore,
} from '@event-driven-io/emmett';
import { getInMemoryEventStore } from '@event-driven-io/emmett';
import {
ApiSpecification,
getApplication,
Expand All @@ -16,8 +13,8 @@ const unitPrice = 100;
const now = new Date();

const given = ApiSpecification.for<ShoppingCartEvent>(
(): EventStore => getInMemoryEventStore(),
(eventStore: EventStore) =>
() => getInMemoryEventStore(),
(eventStore) =>
getApplication({
apis: [
shoppingCartApi(
Expand Down
9 changes: 3 additions & 6 deletions src/docs/snippets/gettingStarted/webApi/apiBDDIntTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
getInMemoryEventStore,
type EventStore,
} from '@event-driven-io/emmett';
import { getInMemoryEventStore } from '@event-driven-io/emmett';
import {
ApiSpecification,
getApplication,
Expand All @@ -18,8 +15,8 @@ const now = new Date();
const unitPrice = Math.random() * 10;

const given = ApiSpecification.for<ShoppingCartEvent>(
(): EventStore => getInMemoryEventStore(),
(eventStore: EventStore) =>
() => getInMemoryEventStore(),
(eventStore) =>
getApplication({
apis: [
shoppingCartApi(
Expand Down
52 changes: 26 additions & 26 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import supertest, { type Response } from 'supertest';

import type { EventStore } from '@event-driven-io/emmett';
import { WrapEventStore } from '@event-driven-io/emmett';
import assert from 'assert';
import type { Application } from 'express';
import type { TestRequest } from './apiSpecification';
Expand All @@ -17,13 +16,13 @@ export type ApiE2ESpecification = (...givenRequests: TestRequest[]) => {
};

export const ApiE2ESpecification = {
for: (
getEventStore: () => EventStore,
getApplication: (eventStore: EventStore) => Application,
for: <Store extends EventStore = EventStore>(
getEventStore: () => Store,
getApplication: (eventStore: Store) => Application,
): ApiE2ESpecification => {
{
return (...givenRequests: TestRequest[]) => {
const eventStore = WrapEventStore(getEventStore());
const eventStore = getEventStore();
const application = getApplication(eventStore);

return {
Expand Down
6 changes: 3 additions & 3 deletions src/packages/emmett-expressjs/src/testing/apiSpecification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ export type ApiSpecification<EventType extends Event = Event> = (
};

export const ApiSpecification = {
for: <EventType extends Event = Event>(
getEventStore: () => EventStore,
getApplication: (eventStore: EventStore) => Application,
for: <EventType extends Event = Event, Store extends EventStore = EventStore>(
getEventStore: () => Store,
getApplication: (eventStore: Store) => Application,
): ApiSpecification<EventType> => {
{
return (...givenStreams: TestEventStream<EventType>[]) => {
Expand Down
2 changes: 1 addition & 1 deletion src/packages/emmett-sqlite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@types/sqlite3": "^3.1.11"
},
"peerDependencies": {
"@event-driven-io/emmett": "^0.23.0-alpha.7",
"@event-driven-io/emmett": "0.23.0-alpha.7",
"sqlite3": "^5.1.7"
}
}
1 change: 1 addition & 0 deletions src/packages/emmett/src/testing/wrapEventStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const WrapEventStore = <Store extends EventStore>(
const appendedEvents = new Map<string, TestEventStream>();

const wrapped = {
...eventStore,
aggregateStream<State, EventType extends Event>(
streamName: string,
options: AggregateStreamOptions<State, EventType>,
Expand Down

0 comments on commit 6a18731

Please sign in to comment.