@@ -4,6 +4,14 @@ import { NestFactory } from '@nestjs/core';
4
4
import { Controller , Get , Module } from '@nestjs/common' ;
5
5
import supertest from 'supertest' ;
6
6
7
+ function providerToken ( name : string ) {
8
+ return `${ name } ProviderToken` ;
9
+ }
10
+
11
+ function pluginInitializedToken ( name : string ) {
12
+ return `${ name . toLocaleUpperCase ( ) } _PLUGIN_INITIALIZED` ;
13
+ }
14
+
7
15
function createDummyPlugin ( name : string ) {
8
16
const watchers = {
9
17
initHasRun : false ,
@@ -18,11 +26,11 @@ function createDummyPlugin(name: string) {
18
26
watchers . destroyHasRun = true ;
19
27
} ,
20
28
onClsInit : ( cls ) => {
21
- cls . set ( 'PLUGIN_INITIALIZED' , true ) ;
29
+ cls . set ( pluginInitializedToken ( name ) , true ) ;
22
30
} ,
23
31
providers : [
24
32
{
25
- provide : 'providerFromPlugin' ,
33
+ provide : providerToken ( name ) ,
26
34
useValue : 'valueFromPlugin' ,
27
35
} ,
28
36
] ,
@@ -51,7 +59,7 @@ describe('Plugins', () => {
51
59
await module . init ( ) ;
52
60
53
61
expect ( watchers . initHasRun ) . toBe ( true ) ;
54
- expect ( module . get ( 'providerFromPlugin' ) ) . toBe ( 'valueFromPlugin' ) ;
62
+ expect ( module . get ( providerToken ( 'forRoot' ) ) ) . toBe ( 'valueFromPlugin' ) ;
55
63
expect ( watchers . destroyHasRun ) . toBe ( false ) ;
56
64
57
65
await module . close ( ) ;
@@ -78,7 +86,9 @@ describe('Plugins', () => {
78
86
await module . init ( ) ;
79
87
80
88
expect ( watchers . initHasRun ) . toBe ( true ) ;
81
- expect ( module . get ( 'providerFromPlugin' ) ) . toBe ( 'valueFromPlugin' ) ;
89
+ expect ( module . get ( providerToken ( 'forRootAsync' ) ) ) . toBe (
90
+ 'valueFromPlugin' ,
91
+ ) ;
82
92
expect ( watchers . destroyHasRun ) . toBe ( false ) ;
83
93
84
94
await module . close ( ) ;
@@ -96,7 +106,7 @@ describe('Plugins', () => {
96
106
constructor ( private readonly cls : ClsService ) { }
97
107
@Get ( )
98
108
get ( ) {
99
- return this . cls . get ( 'PLUGIN_INITIALIZED' ) ;
109
+ return this . cls . get ( pluginInitializedToken ( 'onClsInit' ) ) ;
100
110
}
101
111
}
102
112
@@ -123,4 +133,60 @@ describe('Plugins', () => {
123
133
await module . close ( ) ;
124
134
} ,
125
135
) ;
136
+
137
+ it ( 'should register plugin and run module lifecycle and onClsInit methods (registerPlugins)' , async ( ) => {
138
+ const root = createDummyPlugin ( 'root' ) ;
139
+ const feature = createDummyPlugin ( 'feature' ) ;
140
+
141
+ @Controller ( )
142
+ class TestController {
143
+ constructor ( private readonly cls : ClsService ) { }
144
+ @Get ( )
145
+ get ( ) {
146
+ return (
147
+ this . cls . get ( pluginInitializedToken ( 'root' ) ) &&
148
+ this . cls . get ( pluginInitializedToken ( 'feature' ) )
149
+ ) ;
150
+ }
151
+ }
152
+ @Module ( {
153
+ imports : [
154
+ ClsModule . forRoot ( {
155
+ middleware : {
156
+ mount : true ,
157
+ } ,
158
+ plugins : [ root . plugin ] ,
159
+ } ) ,
160
+ ClsModule . registerPlugins ( [ feature . plugin ] ) ,
161
+ ] ,
162
+ controllers : [ TestController ] ,
163
+ } )
164
+ class TestAppModule { }
165
+
166
+ const module = await NestFactory . create ( TestAppModule ) ;
167
+
168
+ expect ( root . watchers . initHasRun ) . toBe ( false ) ;
169
+ expect ( feature . watchers . initHasRun ) . toBe ( false ) ;
170
+
171
+ await module . init ( ) ;
172
+
173
+ expect ( root . watchers . initHasRun ) . toBe ( true ) ;
174
+ expect ( feature . watchers . initHasRun ) . toBe ( true ) ;
175
+
176
+ expect ( module . get ( providerToken ( 'root' ) ) ) . toBe ( 'valueFromPlugin' ) ;
177
+ expect ( module . get ( providerToken ( 'feature' ) ) ) . toBe ( 'valueFromPlugin' ) ;
178
+
179
+ expect ( root . watchers . destroyHasRun ) . toBe ( false ) ;
180
+ expect ( feature . watchers . destroyHasRun ) . toBe ( false ) ;
181
+
182
+ await supertest ( module . getHttpServer ( ) )
183
+ . get ( '/' )
184
+ . expect ( 200 )
185
+ . expect ( 'true' ) ;
186
+
187
+ await module . close ( ) ;
188
+
189
+ expect ( root . watchers . destroyHasRun ) . toBe ( true ) ;
190
+ expect ( feature . watchers . destroyHasRun ) . toBe ( true ) ;
191
+ } ) ;
126
192
} ) ;
0 commit comments