1
1
import { INestApplication } from '@nestjs/common' ;
2
+ import { ContextIdFactory , createContextId } from '@nestjs/core' ;
2
3
import { Test } from '@nestjs/testing' ;
3
4
import { EventEmitter2 } from 'eventemitter2' ;
4
5
import { EventEmitterReadinessWatcher } from '../../lib' ;
@@ -14,11 +15,21 @@ import { EventsControllerConsumer } from '../src/events-controller.consumer';
14
15
import { EventsProviderAliasedConsumer } from '../src/events-provider-aliased.consumer' ;
15
16
import { EventsProviderPrependConsumer } from '../src/events-provider-prepend.consumer' ;
16
17
import { EventsProviderConsumer } from '../src/events-provider.consumer' ;
18
+ import { EventsProviderDurableRequestScopedConsumer } from '../src/events-provider.durable-request-scoped.consumer' ;
17
19
import { EventsProviderRequestScopedConsumer } from '../src/events-provider.request-scoped.consumer' ;
18
20
import { TEST_PROVIDER_TOKEN } from '../src/test-provider' ;
19
21
20
22
describe ( 'EventEmitterModule - e2e' , ( ) => {
21
23
let app : INestApplication ;
24
+ const durableContextId = createContextId ( ) ;
25
+
26
+ beforeAll ( ( ) => {
27
+ ContextIdFactory . apply ( {
28
+ attach : ( contextId , _request ) => info => {
29
+ return info . isTreeDurable ? durableContextId : contextId ;
30
+ } ,
31
+ } ) ;
32
+ } ) ;
22
33
23
34
beforeEach ( async ( ) => {
24
35
const module = await Test . createTestingModule ( {
@@ -95,6 +106,15 @@ describe('EventEmitterModule - e2e', () => {
95
106
) . toEqual ( TEST_EVENT_PAYLOAD ) ;
96
107
} ) ;
97
108
109
+ it ( 'should be able to emit a durable request-scoped event with a single payload' , async ( ) => {
110
+ await app . init ( ) ;
111
+
112
+ expect (
113
+ EventsProviderDurableRequestScopedConsumer . injectedEventPayload
114
+ . objectValue ,
115
+ ) . toEqual ( TEST_EVENT_PAYLOAD ) ;
116
+ } ) ;
117
+
98
118
it ( 'should be able to emit a request-scoped event with a string payload' , async ( ) => {
99
119
await app . init ( ) ;
100
120
@@ -103,6 +123,15 @@ describe('EventEmitterModule - e2e', () => {
103
123
) . toEqual ( TEST_EVENT_STRING_PAYLOAD ) ;
104
124
} ) ;
105
125
126
+ it ( 'should be able to emit a durable request-scoped event with a string payload' , async ( ) => {
127
+ await app . init ( ) ;
128
+
129
+ expect (
130
+ EventsProviderDurableRequestScopedConsumer . injectedEventPayload
131
+ . stringValue ,
132
+ ) . toEqual ( TEST_EVENT_STRING_PAYLOAD ) ;
133
+ } ) ;
134
+
106
135
it ( 'should be able to emit a request-scoped event with multiple payloads' , async ( ) => {
107
136
await app . init ( ) ;
108
137
@@ -111,6 +140,15 @@ describe('EventEmitterModule - e2e', () => {
111
140
) . toEqual ( TEST_EVENT_MULTIPLE_PAYLOAD ) ;
112
141
} ) ;
113
142
143
+ it ( 'should be able to emit a durable request-scoped event with multiple payloads' , async ( ) => {
144
+ await app . init ( ) ;
145
+
146
+ expect (
147
+ EventsProviderDurableRequestScopedConsumer . injectedEventPayload
148
+ . arrayValue ,
149
+ ) . toEqual ( TEST_EVENT_MULTIPLE_PAYLOAD ) ;
150
+ } ) ;
151
+
114
152
it ( 'should work with non array metadata' , async ( ) => {
115
153
await app . init ( ) ;
116
154
@@ -192,6 +230,39 @@ describe('EventEmitterModule - e2e', () => {
192
230
expect ( eventsConsumerRef . eventPayload ) . toEqual ( TEST_EVENT_PAYLOAD ) ;
193
231
} ) ;
194
232
233
+ it ( 'should throw when an unexpected error occurs from durable request scoped and suppressErrors is false' , async ( ) => {
234
+ await app . init ( ) ;
235
+
236
+ const eventEmitter = app . get ( EventEmitter2 ) ;
237
+ expect (
238
+ eventEmitter . emitAsync ( 'error-throwing.durable-request-scoped' ) ,
239
+ ) . rejects . toThrow ( 'This is a test error' ) ;
240
+ } ) ;
241
+
242
+ it ( 'should load durable provider once for different event emissions' , async ( ) => {
243
+ await app . init ( ) ;
244
+ const eventEmitter = app . get ( EventEmitter2 ) ;
245
+ const [ durableInstance ] = await eventEmitter . emitAsync ( 'durable' ) ;
246
+ const [ durableInstance2 ] = await eventEmitter . emitAsync ( 'durable' ) ;
247
+ expect ( durableInstance ) . toBe ( durableInstance2 ) ;
248
+ } ) ;
249
+
250
+ it ( 'should load durable provider once for different event emissions' , async ( ) => {
251
+ await app . init ( ) ;
252
+ const eventEmitter = app . get ( EventEmitter2 ) ;
253
+ const [ durableInstance ] = await eventEmitter . emitAsync ( 'durable' ) ;
254
+ const [ durableInstance2 ] = await eventEmitter . emitAsync ( 'durable' ) ;
255
+ expect ( durableInstance ) . toBe ( durableInstance2 ) ;
256
+ } ) ;
257
+
258
+ it ( 'should load non-durable provider anew for different event emissions' , async ( ) => {
259
+ await app . init ( ) ;
260
+ const eventEmitter = app . get ( EventEmitter2 ) ;
261
+ const [ notDurableInstance ] = await eventEmitter . emitAsync ( 'not-durable' ) ;
262
+ const [ notDurableInstance2 ] = await eventEmitter . emitAsync ( 'not-durable' ) ;
263
+ expect ( notDurableInstance ) . not . toBe ( notDurableInstance2 ) ;
264
+ } ) ;
265
+
195
266
afterEach ( async ( ) => {
196
267
await app . close ( ) ;
197
268
} ) ;
0 commit comments