Skip to content

Commit 41cfbda

Browse files
committed
Typed TestRequest for API integration and E2E tests
Now it'll be possible to provide named setup without the need to reference Supertest in the end project
1 parent 5938195 commit 41cfbda

File tree

4 files changed

+28
-37
lines changed

4 files changed

+28
-37
lines changed

src/docs/snippets/gettingStarted/webApi/apiBDD.e2e.spec.ts

+12-16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ApiE2ESpecification,
1111
expectResponse,
1212
getApplication,
13+
type TestRequest,
1314
} from '@event-driven-io/emmett-expressjs';
1415
import {
1516
EventStoreDBContainer,
@@ -52,24 +53,20 @@ void describe('ShoppingCart E2E', () => {
5253
});
5354

5455
void describe('When opened with product item', () => {
55-
void it('should confirm', () => {
56-
return given((request) =>
57-
request
58-
.post(`/clients/${clientId}/shopping-carts/current/product-items`)
59-
.send(productItem),
60-
)
56+
const openedShoppingCartWithProduct: TestRequest = (request) =>
57+
request
58+
.post(`/clients/${clientId}/shopping-carts/current/product-items`)
59+
.send(productItem);
60+
61+
void it('should confirm', () =>
62+
given(openedShoppingCartWithProduct)
6163
.when((request) =>
6264
request.post(`/clients/${clientId}/shopping-carts/current/confirm`),
6365
)
64-
.then([expectResponse(204)]);
65-
});
66+
.then([expectResponse(204)]));
6667

67-
void it('should return details', () => {
68-
return given((request) =>
69-
request
70-
.post(`/clients/${clientId}/shopping-carts/current/product-items`)
71-
.send(productItem),
72-
)
68+
void it('should return details', () =>
69+
given(openedShoppingCartWithProduct)
7370
.when((request) =>
7471
request.get(`/clients/${clientId}/shopping-carts/current`).send(),
7572
)
@@ -87,8 +84,7 @@ void describe('ShoppingCart E2E', () => {
8784
status: ShoppingCartStatus.Opened,
8885
},
8986
}),
90-
]);
91-
});
87+
]));
9288
});
9389

9490
const now = new Date();

src/docs/snippets/gettingStarted/webApi/apiBDDE2ETest.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { type EventStore } from '@event-driven-io/emmett';
22
import {
33
ApiE2ESpecification,
44
getApplication,
5+
type TestRequest,
56
} from '@event-driven-io/emmett-expressjs';
67
import { randomUUID } from 'node:crypto';
78
import { describe, it } from 'node:test';
@@ -43,12 +44,13 @@ import { expectResponse } from '@event-driven-io/emmett-expressjs';
4344
import type { StartedEventStoreDBContainer } from '@event-driven-io/emmett-testcontainers';
4445

4546
void describe('When opened with product item', () => {
47+
const openedShoppingCartWithProduct: TestRequest = (request) =>
48+
request
49+
.post(`/clients/${clientId}/shopping-carts/current/product-items`)
50+
.send(productItem);
51+
4652
void it('should confirm', () => {
47-
return given((request) =>
48-
request
49-
.post(`/clients/${clientId}/shopping-carts/current/product-items`)
50-
.send(productItem),
51-
)
53+
return given(openedShoppingCartWithProduct)
5254
.when((request) =>
5355
request.post(`/clients/${clientId}/shopping-carts/current/confirm`),
5456
)

src/packages/emmett-expressjs/src/testing/apiE2ESpecification.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { Test } from 'supertest';
21
import supertest, { type Response } from 'supertest';
3-
import type TestAgent from 'supertest/lib/agent';
42

53
import type {
64
DefaultStreamVersionType,
@@ -9,15 +7,14 @@ import type {
97
import assert from 'assert';
108
import type { Application } from 'express';
119
import { WrapEventStore } from './utils';
10+
import type { TestRequest } from './apiSpecification';
1211

1312
export type E2EResponseAssert = (response: Response) => boolean | void;
1413

1514
export type ApiE2ESpecificationAssert = [E2EResponseAssert];
1615

17-
export type ApiE2ESpecification = (
18-
...givenRequests: ((request: TestAgent<supertest.Test>) => Test)[]
19-
) => {
20-
when: (setupRequest: (request: TestAgent<supertest.Test>) => Test) => {
16+
export type ApiE2ESpecification = (...givenRequests: TestRequest[]) => {
17+
when: (setupRequest: TestRequest) => {
2118
then: (verify: ApiE2ESpecificationAssert) => Promise<void>;
2219
};
2320
};
@@ -28,16 +25,12 @@ export const ApiE2ESpecification = {
2825
getApplication: (eventStore: EventStore<StreamVersion>) => Application,
2926
): ApiE2ESpecification => {
3027
{
31-
return (
32-
...givenRequests: ((request: TestAgent<supertest.Test>) => Test)[]
33-
) => {
28+
return (...givenRequests: TestRequest[]) => {
3429
const eventStore = WrapEventStore(getEventStore());
3530
const application = getApplication(eventStore);
3631

3732
return {
38-
when: (
39-
setupRequest: (request: TestAgent<supertest.Test>) => Test,
40-
) => {
33+
when: (setupRequest: TestRequest) => {
4134
const handle = async () => {
4235
for (const requestFn of givenRequests) {
4336
await requestFn(supertest(application));

src/packages/emmett-expressjs/src/testing/apiSpecification.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { WrapEventStore, type TestEventStream } from './utils';
1616
/////////// Setup
1717
////////////////////////////////
1818

19+
export type TestRequest = (request: TestAgent<supertest.Test>) => Test;
20+
1921
export const existingStream = <EventType extends Event = Event>(
2022
streamId: string,
2123
events: EventType[],
@@ -76,7 +78,7 @@ export const expectError = (
7678
export type ApiSpecification<EventType extends Event = Event> = (
7779
...givenStreams: TestEventStream<EventType>[]
7880
) => {
79-
when: (setupRequest: (request: TestAgent<supertest.Test>) => Test) => {
81+
when: (setupRequest: TestRequest) => {
8082
then: (verify: ApiSpecificationAssert<EventType>) => Promise<void>;
8183
};
8284
};
@@ -95,9 +97,7 @@ export const ApiSpecification = {
9597
const application = getApplication(eventStore);
9698

9799
return {
98-
when: (
99-
setupRequest: (request: TestAgent<supertest.Test>) => Test,
100-
) => {
100+
when: (setupRequest: TestRequest) => {
101101
const handle = async () => {
102102
for (const [streamName, events] of givenStreams) {
103103
await eventStore.setup(streamName, events);

0 commit comments

Comments
 (0)