1
- import { INestApplication , Scope } from '@nestjs/common' ;
1
+ import { INestApplication , Injectable , Scope } from '@nestjs/common' ;
2
2
import { Test } from '@nestjs/testing' ;
3
3
import { expect } from 'chai' ;
4
4
import * as request from 'supertest' ;
@@ -17,65 +17,126 @@ class Meta {
17
17
}
18
18
19
19
describe ( 'Transient scope' , ( ) => {
20
- let server ;
21
- let app : INestApplication ;
22
-
23
- before ( async ( ) => {
24
- const module = await Test . createTestingModule ( {
25
- imports : [
26
- HelloModule . forRoot ( {
27
- provide : 'META' ,
28
- useClass : Meta ,
29
- scope : Scope . TRANSIENT ,
30
- } ) ,
31
- ] ,
32
- } ) . compile ( ) ;
33
-
34
- app = module . createNestApplication ( ) ;
35
- server = app . getHttpServer ( ) ;
36
- await app . init ( ) ;
37
- } ) ;
20
+ describe ( 'when transient scope is used' , ( ) => {
21
+ let server : any ;
22
+ let app : INestApplication ;
38
23
39
- describe ( 'when one service is request scoped' , ( ) => {
40
24
before ( async ( ) => {
41
- const performHttpCall = end =>
42
- request ( server )
43
- . get ( '/hello' )
44
- . end ( err => {
45
- if ( err ) return end ( err ) ;
46
- end ( ) ;
47
- } ) ;
48
- await new Promise < any > ( resolve => performHttpCall ( resolve ) ) ;
49
- await new Promise < any > ( resolve => performHttpCall ( resolve ) ) ;
50
- await new Promise < any > ( resolve => performHttpCall ( resolve ) ) ;
51
- } ) ;
25
+ const module = await Test . createTestingModule ( {
26
+ imports : [
27
+ HelloModule . forRoot ( {
28
+ provide : 'META' ,
29
+ useClass : Meta ,
30
+ scope : Scope . TRANSIENT ,
31
+ } ) ,
32
+ ] ,
33
+ } ) . compile ( ) ;
52
34
53
- it ( `should create controller for each request` , ( ) => {
54
- expect ( HelloController . COUNTER ) . to . be . eql ( 3 ) ;
35
+ app = module . createNestApplication ( ) ;
36
+ server = app . getHttpServer ( ) ;
37
+ await app . init ( ) ;
55
38
} ) ;
56
39
57
- it ( `should create service for each request` , ( ) => {
58
- expect ( UsersService . COUNTER ) . to . be . eql ( 3 ) ;
59
- } ) ;
40
+ describe ( 'and when one service is request scoped' , ( ) => {
41
+ before ( async ( ) => {
42
+ const performHttpCall = end =>
43
+ request ( server )
44
+ . get ( '/hello' )
45
+ . end ( err => {
46
+ if ( err ) return end ( err ) ;
47
+ end ( ) ;
48
+ } ) ;
49
+ await new Promise < any > ( resolve => performHttpCall ( resolve ) ) ;
50
+ await new Promise < any > ( resolve => performHttpCall ( resolve ) ) ;
51
+ await new Promise < any > ( resolve => performHttpCall ( resolve ) ) ;
52
+ } ) ;
53
+
54
+ it ( `should create controller for each request` , ( ) => {
55
+ expect ( HelloController . COUNTER ) . to . be . eql ( 3 ) ;
56
+ } ) ;
57
+
58
+ it ( `should create service for each request` , ( ) => {
59
+ expect ( UsersService . COUNTER ) . to . be . eql ( 3 ) ;
60
+ } ) ;
60
61
61
- it ( `should create provider for each inquirer` , ( ) => {
62
- expect ( Meta . COUNTER ) . to . be . eql ( 7 ) ;
62
+ it ( `should create provider for each inquirer` , ( ) => {
63
+ expect ( Meta . COUNTER ) . to . be . eql ( 7 ) ;
64
+ } ) ;
65
+
66
+ it ( `should create transient pipe for each controller (3 requests, 1 static)` , ( ) => {
67
+ expect ( UserByIdPipe . COUNTER ) . to . be . eql ( 4 ) ;
68
+ } ) ;
69
+
70
+ it ( `should create transient interceptor for each controller (3 requests, 1 static)` , ( ) => {
71
+ expect ( Interceptor . COUNTER ) . to . be . eql ( 4 ) ;
72
+ } ) ;
73
+
74
+ it ( `should create transient guard for each controller (3 requests, 1 static)` , ( ) => {
75
+ expect ( Guard . COUNTER ) . to . be . eql ( 4 ) ;
76
+ } ) ;
63
77
} ) ;
64
78
65
- it ( `should create transient pipe for each controller (3 requests, 1 static)` , ( ) => {
66
- expect ( UserByIdPipe . COUNTER ) . to . be . eql ( 4 ) ;
79
+ after ( async ( ) => {
80
+ await app . close ( ) ;
67
81
} ) ;
82
+ } ) ;
83
+
84
+ describe ( 'when there is a nested structure of transient providers' , ( ) => {
85
+ let app : INestApplication ;
68
86
69
- it ( `should create transient interceptor for each controller (3 requests, 1 static)` , ( ) => {
70
- expect ( Interceptor . COUNTER ) . to . be . eql ( 4 ) ;
87
+ @Injectable ( { scope : Scope . TRANSIENT } )
88
+ class DeepTransient {
89
+ public initialized = false ;
90
+
91
+ constructor ( ) {
92
+ this . initialized = true ;
93
+ }
94
+ }
95
+
96
+ @Injectable ( { scope : Scope . TRANSIENT } )
97
+ class LoggerService {
98
+ public context ?: string ;
99
+ }
100
+
101
+ @Injectable ( { scope : Scope . TRANSIENT } )
102
+ class SecondService {
103
+ constructor ( public readonly loggerService : LoggerService ) {
104
+ this . loggerService . context = 'SecondService' ;
105
+ }
106
+ }
107
+
108
+ @Injectable ( )
109
+ class FirstService {
110
+ constructor (
111
+ public readonly secondService : SecondService ,
112
+ public readonly loggerService : LoggerService ,
113
+ public readonly deepTransient : DeepTransient ,
114
+ ) {
115
+ this . loggerService . context = 'FirstService' ;
116
+ }
117
+ }
118
+
119
+ before ( async ( ) => {
120
+ const module = await Test . createTestingModule ( {
121
+ providers : [ FirstService , SecondService , LoggerService , DeepTransient ] ,
122
+ } ) . compile ( ) ;
123
+
124
+ app = module . createNestApplication ( ) ;
125
+ await app . init ( ) ;
71
126
} ) ;
72
127
73
- it ( `should create transient guard for each controller (3 requests, 1 static)` , ( ) => {
74
- expect ( Guard . COUNTER ) . to . be . eql ( 4 ) ;
128
+ it ( 'should create a new instance of the transient provider for each provider' , async ( ) => {
129
+ const firstService1 = app . get ( FirstService ) ;
130
+
131
+ expect ( firstService1 . secondService . loggerService . context ) . to . equal (
132
+ 'SecondService' ,
133
+ ) ;
134
+ expect ( firstService1 . loggerService . context ) . to . equal ( 'FirstService' ) ;
135
+ expect ( firstService1 . deepTransient . initialized ) . to . be . true ;
75
136
} ) ;
76
- } ) ;
77
137
78
- after ( async ( ) => {
79
- await app . close ( ) ;
138
+ after ( async ( ) => {
139
+ await app . close ( ) ;
140
+ } ) ;
80
141
} ) ;
81
142
} ) ;
0 commit comments