5
5
noop ,
6
6
Rule ,
7
7
Tree ,
8
- SchematicContext
8
+ SchematicContext ,
9
+ schematic
9
10
} from '@angular-devkit/schematics' ;
10
11
import { Schema } from './schema' ;
11
12
import * as path from 'path' ;
@@ -33,6 +34,7 @@ import {
33
34
import { formatFiles } from '../../utils/rules/format-files' ;
34
35
import { updateKarmaConf } from '../../utils/rules/update-karma-conf' ;
35
36
import { excludeUnnecessaryFiles } from '@nrwl/schematics/src/utils/rules/filter-tree' ;
37
+ import { join , normalize } from '@angular-devkit/core' ;
36
38
37
39
interface NormalizedSchema extends Schema {
38
40
name : string ;
@@ -240,6 +242,12 @@ function updateProject(options: NormalizedSchema): Rule {
240
242
host . delete ( path . join ( options . projectRoot , 'package.json' ) ) ;
241
243
}
242
244
245
+ if ( options . unitTestRunner !== 'karma' ) {
246
+ host . delete ( path . join ( options . projectRoot , 'karma.conf.js' ) ) ;
247
+ host . delete ( path . join ( options . projectRoot , 'src/test.ts' ) ) ;
248
+ host . delete ( path . join ( options . projectRoot , 'tsconfig.spec.json' ) ) ;
249
+ }
250
+
243
251
host . overwrite (
244
252
path . join ( libRoot , `${ options . name } .module.ts` ) ,
245
253
`
@@ -253,26 +261,29 @@ function updateProject(options: NormalizedSchema): Rule {
253
261
export class ${ options . moduleName } { }
254
262
`
255
263
) ;
256
- host . create (
257
- path . join ( libRoot , `${ options . name } .module.spec.ts` ) ,
258
- `
259
- import { async, TestBed } from '@angular/core/testing';
260
- import { ${ options . moduleName } } from './${ options . name } .module';
261
264
262
- describe('${ options . moduleName } ', () => {
263
- beforeEach(async(() => {
264
- TestBed.configureTestingModule({
265
- imports: [ ${ options . moduleName } ]
266
- })
267
- .compileComponents();
268
- }));
269
-
270
- it('should create', () => {
271
- expect(${ options . moduleName } ).toBeDefined();
265
+ if ( options . unitTestRunner !== 'none' ) {
266
+ host . create (
267
+ path . join ( libRoot , `${ options . name } .module.spec.ts` ) ,
268
+ `
269
+ import { async, TestBed } from '@angular/core/testing';
270
+ import { ${ options . moduleName } } from './${ options . name } .module';
271
+
272
+ describe('${ options . moduleName } ', () => {
273
+ beforeEach(async(() => {
274
+ TestBed.configureTestingModule({
275
+ imports: [ ${ options . moduleName } ]
276
+ })
277
+ .compileComponents();
278
+ }));
279
+
280
+ it('should create', () => {
281
+ expect(${ options . moduleName } ).toBeDefined();
282
+ });
272
283
});
273
- });
274
- `
275
- ) ;
284
+ `
285
+ ) ;
286
+ }
276
287
host . overwrite (
277
288
`${ options . projectRoot } /src/index.ts` ,
278
289
`
@@ -293,6 +304,16 @@ describe('${options.moduleName}', () => {
293
304
delete fixedProject . architect . build ;
294
305
}
295
306
307
+ if ( options . unitTestRunner !== 'karma' ) {
308
+ delete fixedProject . architect . test ;
309
+
310
+ fixedProject . architect . lint . options . tsConfig = fixedProject . architect . lint . options . tsConfig . filter (
311
+ path =>
312
+ path !==
313
+ join ( normalize ( options . projectRoot ) , 'tsconfig.spec.json' )
314
+ ) ;
315
+ }
316
+
296
317
json . projects [ options . name ] = fixedProject ;
297
318
return json ;
298
319
} ) ,
@@ -309,18 +330,6 @@ describe('${options.moduleName}', () => {
309
330
}
310
331
} ;
311
332
} ) ,
312
- updateJsonInTree ( `${ options . projectRoot } /tsconfig.spec.json` , json => {
313
- return {
314
- ...json ,
315
- extends : `${ offsetFromRoot ( options . projectRoot ) } tsconfig.json` ,
316
- compilerOptions : {
317
- ...json . compilerOptions ,
318
- outDir : `${ offsetFromRoot ( options . projectRoot ) } dist/out-tsc/${
319
- options . projectRoot
320
- } `
321
- }
322
- } ;
323
- } ) ,
324
333
updateJsonInTree ( `${ options . projectRoot } /tslint.json` , json => {
325
334
return {
326
335
...json ,
@@ -337,22 +346,43 @@ describe('${options.moduleName}', () => {
337
346
} ;
338
347
} ) ,
339
348
updateNgPackage ( options ) ,
340
- host => {
341
- const karma = host
342
- . read ( `${ options . projectRoot } /karma.conf.js` )
343
- . toString ( ) ;
344
- host . overwrite (
345
- `${ options . projectRoot } /karma.conf.js` ,
346
- karma . replace (
347
- `'../../coverage${ options . projectRoot } '` ,
348
- `'${ offsetFromRoot ( options . projectRoot ) } coverage'`
349
- )
350
- ) ;
351
- }
349
+ options . unitTestRunner === 'karma' ? updateKarmaConfig ( options ) : noop ( )
352
350
] ) ( host , null ) ;
353
351
} ;
354
352
}
355
353
354
+ function updateKarmaConfig ( options : NormalizedSchema ) {
355
+ return chain ( [
356
+ host => {
357
+ const karma = host
358
+ . read ( `${ options . projectRoot } /karma.conf.js` )
359
+ . toString ( ) ;
360
+ host . overwrite (
361
+ `${ options . projectRoot } /karma.conf.js` ,
362
+ karma . replace (
363
+ `'../../coverage${ options . projectRoot } '` ,
364
+ `'${ offsetFromRoot ( options . projectRoot ) } coverage'`
365
+ )
366
+ ) ;
367
+ } ,
368
+ updateJsonInTree ( `${ options . projectRoot } /tsconfig.spec.json` , json => {
369
+ return {
370
+ ...json ,
371
+ extends : `${ offsetFromRoot ( options . projectRoot ) } tsconfig.json` ,
372
+ compilerOptions : {
373
+ ...json . compilerOptions ,
374
+ outDir : `${ offsetFromRoot ( options . projectRoot ) } dist/out-tsc/${
375
+ options . projectRoot
376
+ } `
377
+ }
378
+ } ;
379
+ } ) ,
380
+ updateKarmaConf ( {
381
+ projectName : options . name
382
+ } )
383
+ ] ) ;
384
+ }
385
+
356
386
function updateTsConfig ( options : NormalizedSchema ) : Rule {
357
387
return chain ( [
358
388
( host : Tree , context : SchematicContext ) => {
@@ -396,10 +426,12 @@ export default function(schema: Schema): Rule {
396
426
397
427
move ( options . name , options . projectRoot ) ,
398
428
updateProject ( options ) ,
399
- updateKarmaConf ( {
400
- projectName : options . name
401
- } ) ,
402
429
updateTsConfig ( options ) ,
430
+ options . unitTestRunner === 'jest'
431
+ ? schematic ( 'jest-project' , {
432
+ project : options . name
433
+ } )
434
+ : noop ( ) ,
403
435
404
436
options . publishable ? updateLibPackageNpmScope ( options ) : noop ( ) ,
405
437
options . routing && options . lazy
0 commit comments