1
- import { Tree } from '@angular-devkit/schematics' ;
1
+ import {
2
+ Tree ,
3
+ FileEntry ,
4
+ CircularCollectionException
5
+ } from '@angular-devkit/schematics' ;
2
6
import {
3
7
SchematicTestRunner ,
4
8
UnitTestTree
5
9
} from '@angular-devkit/schematics/testing' ;
6
10
import * as path from 'path' ;
7
11
import { Schema } from './schema' ;
8
12
13
+ const getFileContent = ( tree : Tree , path : string ) : string => {
14
+ const fileEntry : FileEntry = tree . get ( path ) ;
15
+ if ( ! fileEntry ) {
16
+ throw new Error ( `The file does not exist.` ) ;
17
+ }
18
+ return fileEntry . content . toString ( ) ;
19
+ } ;
9
20
describe ( 'Schematic Tests Nest Add' , ( ) => {
10
21
let nestTree : Tree ;
11
22
@@ -34,9 +45,9 @@ describe('Schematic Tests Nest Add', () => {
34
45
expect ( files ) . toEqual ( [
35
46
'/.eslintrc.js' ,
36
47
'/.prettierrc' ,
37
- '/README.md' ,
38
48
'/nest-cli.json' ,
39
49
'/package.json' ,
50
+ '/README.md' ,
40
51
'/tsconfig.build.json' ,
41
52
'/tsconfig.json' ,
42
53
'/.funcignore' ,
@@ -83,9 +94,9 @@ describe('Schematic Tests Nest Add', () => {
83
94
expect ( files ) . toEqual ( [
84
95
'/.eslintrc.js' ,
85
96
'/.prettierrc' ,
86
- '/README.md' ,
87
97
'/nest-cli.json' ,
88
98
'/package.json' ,
99
+ '/README.md' ,
89
100
'/tsconfig.build.json' ,
90
101
'/tsconfig.json' ,
91
102
'/src/app.controller.spec.ts' ,
@@ -105,12 +116,242 @@ describe('Schematic Tests Nest Add', () => {
105
116
'/libs/lib1/src/local.settings.json' ,
106
117
'/libs/lib1/src/main.azure.ts' ,
107
118
'/libs/lib1/src/proxies.json' ,
119
+ '/libs/lib1/src/webpack.config.js' ,
108
120
'/libs/lib1/src/main/function.json' ,
109
121
'/libs/lib1/src/main/index.ts' ,
110
122
'/libs/lib1/src/main/sample.dat'
111
123
] ) ;
112
124
} ) ;
113
125
126
+ it ( 'should have a nest-cli.json for project' , async ( ) => {
127
+ const options : Schema = {
128
+ sourceRoot : '/libs/lib1/src' ,
129
+ skipInstall : true ,
130
+ rootDir : 'src' ,
131
+ project : 'lib1'
132
+ } ;
133
+ await runner
134
+ . runExternalSchematicAsync (
135
+ '@nestjs/schematics' ,
136
+ 'library' ,
137
+ {
138
+ name : 'lib1' ,
139
+ prefix : '@app'
140
+ } ,
141
+ nestTree
142
+ )
143
+ . toPromise ( ) ;
144
+
145
+ const tree = await runner
146
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
147
+ . toPromise ( ) ;
148
+ const fileContent = getFileContent ( tree , '/nest-cli.json' ) ;
149
+ const parsedFile = JSON . parse ( fileContent ) ;
150
+ expect ( parsedFile . projects . lib1 . sourceRoot ) . toEqual ( 'libs/lib1/src' ) ;
151
+ } ) ;
152
+
153
+ it ( 'should a nest-cli.json for default app' , async ( ) => {
154
+ const options : Schema = {
155
+ sourceRoot : 'src' ,
156
+ skipInstall : true ,
157
+ rootDir : 'src' ,
158
+ rootModuleFileName : 'app.module' ,
159
+ rootModuleClassName : 'AppModule'
160
+ } ;
161
+
162
+ const tree = await runner
163
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
164
+ . toPromise ( ) ;
165
+
166
+ const fileContent = getFileContent ( tree , '/nest-cli.json' ) ;
167
+ expect ( fileContent ) . toContain ( `"sourceRoot": "src"` ) ;
168
+ } ) ;
169
+
170
+ it ( 'should import the app.module int main azure file for project' , async ( ) => {
171
+ const options : Schema = {
172
+ sourceRoot : '/libs/lib1/src' ,
173
+ skipInstall : true ,
174
+ rootDir : 'src' ,
175
+ project : 'lib1'
176
+ } ;
177
+ await runner
178
+ . runExternalSchematicAsync (
179
+ '@nestjs/schematics' ,
180
+ 'library' ,
181
+ {
182
+ name : 'lib1' ,
183
+ prefix : '@app'
184
+ } ,
185
+ nestTree
186
+ )
187
+ . toPromise ( ) ;
188
+
189
+ const tree = await runner
190
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
191
+ . toPromise ( ) ;
192
+ const fileContent = getFileContent ( tree , '/libs/lib1/src/main.azure.ts' ) ;
193
+
194
+ expect ( fileContent ) . toContain ( `import { AppModule } from './app.module';` ) ;
195
+ } ) ;
196
+
197
+ it ( 'should import the app.module int main azure file for default app' , async ( ) => {
198
+ const options : Schema = {
199
+ sourceRoot : 'src' ,
200
+ skipInstall : true ,
201
+ rootDir : 'src' ,
202
+ rootModuleFileName : 'app.module' ,
203
+ rootModuleClassName : 'AppModule'
204
+ } ;
205
+
206
+ const tree = await runner
207
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
208
+ . toPromise ( ) ;
209
+
210
+ const fileContent = getFileContent ( tree , '/src/main.azure.ts' ) ;
211
+
212
+ expect ( fileContent ) . toContain ( `import { AppModule } from './app.module';` ) ;
213
+ } ) ;
214
+
215
+ it ( 'should have the root dir for index file in main azure dir for project' , async ( ) => {
216
+ const options : Schema = {
217
+ sourceRoot : '/libs/lib1/src' ,
218
+ skipInstall : true ,
219
+ rootDir : 'src' ,
220
+ project : 'lib1'
221
+ } ;
222
+ await runner
223
+ . runExternalSchematicAsync (
224
+ '@nestjs/schematics' ,
225
+ 'library' ,
226
+ {
227
+ name : 'lib1' ,
228
+ prefix : '@app'
229
+ } ,
230
+ nestTree
231
+ )
232
+ . toPromise ( ) ;
233
+
234
+ const tree = await runner
235
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
236
+ . toPromise ( ) ;
237
+ const fileContent = getFileContent ( tree , '/libs/lib1/src/main/index.ts' ) ;
238
+
239
+ expect ( fileContent ) . toContain ( `import { createApp } from '../main.azure';` ) ;
240
+ } ) ;
241
+
242
+ it ( 'should have the root dir for index file in main azure dir for default app' , async ( ) => {
243
+ const options : Schema = {
244
+ sourceRoot : 'src' ,
245
+ skipInstall : true ,
246
+ rootDir : 'src' ,
247
+ rootModuleFileName : 'app.module' ,
248
+ rootModuleClassName : 'AppModule'
249
+ } ;
250
+
251
+ const tree = await runner
252
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
253
+ . toPromise ( ) ;
254
+
255
+ const fileContent = getFileContent ( tree , '/main/index.ts' ) ;
256
+
257
+ expect ( fileContent ) . toContain (
258
+ `import { createApp } from '../src/main.azure';`
259
+ ) ;
260
+ } ) ;
261
+
262
+ it ( 'should import the webpack config for a project' , async ( ) => {
263
+ const options : Schema = {
264
+ sourceRoot : '/libs/lib1/src' ,
265
+ skipInstall : true ,
266
+ rootDir : 'src' ,
267
+ project : 'lib1'
268
+ } ;
269
+ await runner
270
+ . runExternalSchematicAsync (
271
+ '@nestjs/schematics' ,
272
+ 'library' ,
273
+ {
274
+ name : 'lib1' ,
275
+ prefix : '@app'
276
+ } ,
277
+ nestTree
278
+ )
279
+ . toPromise ( ) ;
280
+
281
+ const tree = await runner
282
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
283
+ . toPromise ( ) ;
284
+
285
+ const fileContent = getFileContent (
286
+ tree ,
287
+ '/libs/lib1/src/webpack.config.js'
288
+ ) ;
289
+ expect ( fileContent ) . toContain ( `filename: 'apps/lib1/main/index.js'` ) ;
290
+ } ) ;
291
+
292
+ it ( 'should not import the webpack config for a default app' , async ( ) => {
293
+ const options : Schema = {
294
+ sourceRoot : 'src' ,
295
+ skipInstall : true ,
296
+ rootDir : 'src' ,
297
+ rootModuleFileName : 'app.module' ,
298
+ rootModuleClassName : 'AppModule'
299
+ } ;
300
+
301
+ const tree = await runner
302
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
303
+ . toPromise ( ) ;
304
+
305
+ const fileContent = tree . get ( 'webpack.config.js' ) ;
306
+
307
+ expect ( fileContent ) . toBeNull ( ) ;
308
+ } ) ;
309
+
310
+ fit ( 'should add a custom webpack config to the compilerOptions for a project' , async ( ) => {
311
+ const options : Schema = {
312
+ sourceRoot : '/apps/azure-func-http/src' ,
313
+ skipInstall : true ,
314
+ rootDir : '' ,
315
+ project : 'azure-func-http'
316
+ } ;
317
+ // await runner
318
+ // .runExternalSchematicAsync(
319
+ // '@nestjs/schematics',
320
+ // 'application',
321
+ // {
322
+ // name: 'azure-func-http',
323
+ // prefix: '@app'
324
+ // },
325
+ // nestTree
326
+ // )
327
+ // .toPromise();
328
+
329
+ await runner
330
+ . runExternalSchematicAsync (
331
+ '@nestjs/schematics' ,
332
+ 'sub-app' ,
333
+ {
334
+ name : 'azure-func-http'
335
+ } ,
336
+ nestTree
337
+ )
338
+ . toPromise ( ) ;
339
+
340
+ const tree = await runner
341
+ . runSchematicAsync ( 'nest-add' , options , nestTree )
342
+ . toPromise ( ) ;
343
+
344
+ const fileContent = getFileContent ( tree , 'nest-cli.json' ) ;
345
+ const parsedFile = JSON . parse ( fileContent ) ;
346
+
347
+ const compilerOptions = parsedFile . projects . lib1 . compilerOptions ;
348
+ expect ( compilerOptions ) . toContain ( {
349
+ webpack : 'true' ,
350
+ webpackConfigPath : 'apps/lib1/src/webpack.config.js' ,
351
+ tsConfigPath : 'libs/lib1/tsconfig.lib.json'
352
+ } ) ;
353
+ } ) ;
354
+
114
355
async function createTestNest (
115
356
runner : SchematicTestRunner ,
116
357
tree ?: Tree
0 commit comments