| 
 | 1 | +/**  | 
 | 2 | + * @license  | 
 | 3 | + * Copyright Google LLC All Rights Reserved.  | 
 | 4 | + *  | 
 | 5 | + * Use of this source code is governed by an MIT-style license that can be  | 
 | 6 | + * found in the LICENSE file at https://angular.dev/license  | 
 | 7 | + */  | 
 | 8 | + | 
 | 9 | +import { isJsonObject } from '@angular-devkit/core';  | 
 | 10 | +import { EmptyTree } from '@angular-devkit/schematics';  | 
 | 11 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';  | 
 | 12 | +import { Builders, ProjectType, WorkspaceSchema } from '../../utility/workspace-models';  | 
 | 13 | + | 
 | 14 | +describe('Migration to update moduleResolution', () => {  | 
 | 15 | +  const schematicName = 'update-module-resolution';  | 
 | 16 | +  const schematicRunner = new SchematicTestRunner(  | 
 | 17 | +    'migrations',  | 
 | 18 | +    require.resolve('../migration-collection.json'),  | 
 | 19 | +  );  | 
 | 20 | + | 
 | 21 | +  function createJsonFile(tree: UnitTestTree, filePath: string, content: {}): void {  | 
 | 22 | +    const stringifiedContent = JSON.stringify(content, undefined, 2);  | 
 | 23 | +    if (tree.exists(filePath)) {  | 
 | 24 | +      tree.overwrite(filePath, stringifiedContent);  | 
 | 25 | +    } else {  | 
 | 26 | +      tree.create(filePath, stringifiedContent);  | 
 | 27 | +    }  | 
 | 28 | +  }  | 
 | 29 | + | 
 | 30 | +  function getCompilerOptionsValue(tree: UnitTestTree, filePath: string): Record<string, unknown> {  | 
 | 31 | +    const json = tree.readJson(filePath);  | 
 | 32 | +    if (isJsonObject(json) && isJsonObject(json.compilerOptions)) {  | 
 | 33 | +      return json.compilerOptions;  | 
 | 34 | +    }  | 
 | 35 | + | 
 | 36 | +    throw new Error(`Cannot retrieve 'compilerOptions'.`);  | 
 | 37 | +  }  | 
 | 38 | + | 
 | 39 | +  const angularConfig: WorkspaceSchema = {  | 
 | 40 | +    version: 1,  | 
 | 41 | +    projects: {  | 
 | 42 | +      app: {  | 
 | 43 | +        root: '',  | 
 | 44 | +        sourceRoot: 'src',  | 
 | 45 | +        projectType: ProjectType.Application,  | 
 | 46 | +        prefix: 'app',  | 
 | 47 | +        architect: {  | 
 | 48 | +          build: {  | 
 | 49 | +            builder: Builders.Browser,  | 
 | 50 | +            options: {  | 
 | 51 | +              tsConfig: 'src/tsconfig.app.json',  | 
 | 52 | +              main: '',  | 
 | 53 | +              polyfills: '',  | 
 | 54 | +            },  | 
 | 55 | +            configurations: {  | 
 | 56 | +              production: {  | 
 | 57 | +                tsConfig: 'src/tsconfig.app.prod.json',  | 
 | 58 | +              },  | 
 | 59 | +            },  | 
 | 60 | +          },  | 
 | 61 | +          test: {  | 
 | 62 | +            builder: Builders.Karma,  | 
 | 63 | +            options: {  | 
 | 64 | +              karmaConfig: '',  | 
 | 65 | +              tsConfig: 'src/tsconfig.spec.json',  | 
 | 66 | +            },  | 
 | 67 | +          },  | 
 | 68 | +        },  | 
 | 69 | +      },  | 
 | 70 | +    },  | 
 | 71 | +  };  | 
 | 72 | + | 
 | 73 | +  let tree: UnitTestTree;  | 
 | 74 | +  beforeEach(() => {  | 
 | 75 | +    tree = new UnitTestTree(new EmptyTree());  | 
 | 76 | +    const compilerOptions = { module: 'es2020', moduleResolution: 'node' };  | 
 | 77 | +    const configWithExtends = { extends: './tsconfig.json', compilerOptions };  | 
 | 78 | + | 
 | 79 | +    // Workspace  | 
 | 80 | +    createJsonFile(tree, 'angular.json', angularConfig);  | 
 | 81 | +    createJsonFile(tree, 'tsconfig.json', { compilerOptions });  | 
 | 82 | + | 
 | 83 | +    // Application  | 
 | 84 | +    createJsonFile(tree, 'src/tsconfig.app.json', configWithExtends);  | 
 | 85 | +    createJsonFile(tree, 'src/tsconfig.app.prod.json', configWithExtends);  | 
 | 86 | +    createJsonFile(tree, 'src/tsconfig.spec.json', { compilerOptions });  | 
 | 87 | +  });  | 
 | 88 | + | 
 | 89 | +  it(`should update moduleResolution to 'bundler' in workspace 'tsconfig.json'`, async () => {  | 
 | 90 | +    const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);  | 
 | 91 | +    const compilerOptions = getCompilerOptionsValue(newTree, 'tsconfig.json');  | 
 | 92 | +    expect(compilerOptions).toEqual(  | 
 | 93 | +      jasmine.objectContaining({  | 
 | 94 | +        moduleResolution: 'bundler',  | 
 | 95 | +      }),  | 
 | 96 | +    );  | 
 | 97 | +  });  | 
 | 98 | + | 
 | 99 | +  it(`should update moduleResolution to 'bundler' in builder tsconfig`, async () => {  | 
 | 100 | +    const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);  | 
 | 101 | +    const compilerOptions = getCompilerOptionsValue(newTree, 'src/tsconfig.spec.json');  | 
 | 102 | +    expect(compilerOptions).toEqual(  | 
 | 103 | +      jasmine.objectContaining({  | 
 | 104 | +        moduleResolution: 'bundler',  | 
 | 105 | +      }),  | 
 | 106 | +    );  | 
 | 107 | +  });  | 
 | 108 | + | 
 | 109 | +  it('should not update moduleResolution when module is preserve', async () => {  | 
 | 110 | +    createJsonFile(tree, 'tsconfig.json', {  | 
 | 111 | +      compilerOptions: { module: 'preserve', moduleResolution: 'node' },  | 
 | 112 | +    });  | 
 | 113 | + | 
 | 114 | +    const newTree = await schematicRunner.runSchematic(schematicName, {}, tree);  | 
 | 115 | +    const compilerOptions = getCompilerOptionsValue(newTree, 'tsconfig.json');  | 
 | 116 | +    expect(compilerOptions).toEqual({ module: 'preserve', moduleResolution: 'node' });  | 
 | 117 | +  });  | 
 | 118 | +});  | 
0 commit comments