66 * found in the LICENSE file at https://angular.dev/license
77 */
88
9+ import { isJsonObject } from '@angular-devkit/core' ;
910import { EmptyTree } from '@angular-devkit/schematics' ;
1011import { SchematicTestRunner , UnitTestTree } from '@angular-devkit/schematics/testing' ;
1112import { Builders , ProjectType , WorkspaceSchema } from '../../utility/workspace-models' ;
@@ -36,7 +37,10 @@ function createWorkSpaceConfig(tree: UnitTestTree) {
3637 } ;
3738
3839 tree . create ( '/angular.json' , JSON . stringify ( angularConfig , undefined , 2 ) ) ;
39- tree . create ( '/tsconfig.json' , JSON . stringify ( { } , undefined , 2 ) ) ;
40+ tree . create (
41+ '/tsconfig.json' ,
42+ JSON . stringify ( { compilerOptions : { module : 'preserve' } } , undefined , 2 ) ,
43+ ) ;
4044 tree . create ( '/package.json' , JSON . stringify ( { } , undefined , 2 ) ) ;
4145}
4246
@@ -49,6 +53,23 @@ function addWorkspaceTarget(tree: UnitTestTree, targetName: string, targetEntry:
4953 tree . overwrite ( '/angular.json' , JSON . stringify ( workspaceContent ) ) ;
5054}
5155
56+ function getCompilerOptionsValue ( tree : UnitTestTree , filePath : string ) : Record < string , unknown > {
57+ const json = tree . readJson ( filePath ) ;
58+ if ( isJsonObject ( json ) && isJsonObject ( json . compilerOptions ) ) {
59+ return json . compilerOptions ;
60+ }
61+
62+ throw new Error ( `Cannot retrieve 'compilerOptions'.` ) ;
63+ }
64+
65+ function createJsonFile ( tree : UnitTestTree , filePath : string , content : { } ) : void {
66+ const stringifiedContent = JSON . stringify ( content , undefined , 2 ) ;
67+ if ( tree . exists ( filePath ) ) {
68+ tree . overwrite ( filePath , stringifiedContent ) ;
69+ } else {
70+ tree . create ( filePath , stringifiedContent ) ;
71+ }
72+ }
5273describe ( `Migration to use the application builder` , ( ) => {
5374 const schematicName = 'use-application-builder' ;
5475 const schematicRunner = new SchematicTestRunner (
@@ -448,4 +469,14 @@ describe(`Migration to use the application builder`, () => {
448469
449470 expect ( devDependencies [ 'postcss' ] ) . toBeUndefined ( ) ;
450471 } ) ;
472+
473+ it ( 'it should not add esModuleInterop and moduleResolution when module is preserve' , async ( ) => {
474+ createJsonFile ( tree , 'tsconfig.json' , {
475+ compilerOptions : { module : 'preserve' } ,
476+ } ) ;
477+ const newTree = await schematicRunner . runSchematic ( schematicName , { } , tree ) ;
478+
479+ const compilerOptions = getCompilerOptionsValue ( newTree , 'tsconfig.json' ) ;
480+ expect ( compilerOptions ) . toEqual ( { module : 'preserve' } ) ;
481+ } ) ;
451482} ) ;
0 commit comments