|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. 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.io/license |
| 7 | + */ |
| 8 | +import { Architect } from '@angular-devkit/architect'; |
| 9 | +import { TestLogger } from '@angular-devkit/architect/testing'; |
| 10 | +import { debounceTime, take, tap } from 'rxjs/operators'; |
| 11 | +import { BrowserBuilderOutput } from '../../src/browser/index'; |
| 12 | +import { createArchitect, host, ivyEnabled } from '../utils'; |
| 13 | + |
| 14 | + |
| 15 | +describe('Browser Builder unused files warnings', () => { |
| 16 | + const warningMessageSuffix = `is part of the TypeScript compilation but it's unused`; |
| 17 | + const targetSpec = { project: 'app', target: 'build' }; |
| 18 | + let architect: Architect; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + await host.initialize().toPromise(); |
| 22 | + |
| 23 | + architect = (await createArchitect(host.root())).architect; |
| 24 | + }); |
| 25 | + afterEach(async () => host.restore().toPromise()); |
| 26 | + |
| 27 | + it('should not show warning when all files are used', async () => { |
| 28 | + if (!ivyEnabled) { |
| 29 | + // TODO: https://github.com/angular/angular-cli/issues/15056 |
| 30 | + pending('Only supported in Ivy.'); |
| 31 | + |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + const logger = new TestLogger('unused-files-warnings'); |
| 36 | + const run = await architect.scheduleTarget(targetSpec, undefined, { logger }); |
| 37 | + const output = await run.result as BrowserBuilderOutput; |
| 38 | + expect(output.success).toBe(true); |
| 39 | + expect(logger.includes(warningMessageSuffix)).toBe(false); |
| 40 | + logger.clear(); |
| 41 | + |
| 42 | + await run.stop(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should show warning when some files are unused', async () => { |
| 46 | + if (!ivyEnabled) { |
| 47 | + // TODO: https://github.com/angular/angular-cli/issues/15056 |
| 48 | + pending('Only supported in Ivy.'); |
| 49 | + |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + host.replaceInFile( |
| 54 | + 'src/tsconfig.app.json', |
| 55 | + '"main.ts"', |
| 56 | + '"main.ts", "environments/environment.prod.ts"', |
| 57 | + ); |
| 58 | + |
| 59 | + const logger = new TestLogger('unused-files-warnings'); |
| 60 | + const run = await architect.scheduleTarget(targetSpec, undefined, { logger }); |
| 61 | + const output = await run.result as BrowserBuilderOutput; |
| 62 | + expect(output.success).toBe(true); |
| 63 | + expect(logger.includes(`environment.prod.ts ${warningMessageSuffix}`)).toBe(true); |
| 64 | + logger.clear(); |
| 65 | + |
| 66 | + await run.stop(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should not show warning when type files are used', async () => { |
| 70 | + if (!ivyEnabled) { |
| 71 | + // TODO: https://github.com/angular/angular-cli/issues/15056 |
| 72 | + pending('Only supported in Ivy.'); |
| 73 | + |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + host.writeMultipleFiles({ |
| 78 | + 'src/app/type.ts': 'export type MyType = number;' |
| 79 | + }); |
| 80 | + |
| 81 | + host.replaceInFile( |
| 82 | + 'src/app/app.component.ts', |
| 83 | + `'@angular/core';`, |
| 84 | + `'@angular/core';\nimport { MyType } from './type';\n` |
| 85 | + ); |
| 86 | + |
| 87 | + const logger = new TestLogger('unused-files-warnings'); |
| 88 | + const run = await architect.scheduleTarget(targetSpec, undefined, { logger }); |
| 89 | + const output = await run.result as BrowserBuilderOutput; |
| 90 | + expect(output.success).toBe(true); |
| 91 | + expect(logger.includes(warningMessageSuffix)).toBe(false); |
| 92 | + logger.clear(); |
| 93 | + |
| 94 | + await run.stop(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('works for rebuilds', async () => { |
| 98 | + if (!ivyEnabled) { |
| 99 | + // TODO: https://github.com/angular/angular-cli/issues/15056 |
| 100 | + pending('Only supported in Ivy.'); |
| 101 | + |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + host.replaceInFile( |
| 106 | + 'src/tsconfig.app.json', |
| 107 | + '"**/*.d.ts"', |
| 108 | + '"**/*.d.ts", "testing/**/*.ts"', |
| 109 | + ); |
| 110 | + |
| 111 | + const logger = new TestLogger('unused-files-warnings'); |
| 112 | + let buildNumber = 0; |
| 113 | + const run = await architect.scheduleTarget(targetSpec, { watch: true }, { logger }); |
| 114 | + |
| 115 | + await run.output |
| 116 | + .pipe( |
| 117 | + debounceTime(1000), |
| 118 | + tap(buildEvent => { |
| 119 | + expect(buildEvent.success).toBe(true); |
| 120 | + |
| 121 | + buildNumber ++; |
| 122 | + switch (buildNumber) { |
| 123 | + case 1: |
| 124 | + // The first should not have unused files |
| 125 | + expect(logger.includes(warningMessageSuffix)).toBe(false, `Case ${buildNumber} failed.`); |
| 126 | + |
| 127 | + // Write a used file |
| 128 | + host.writeMultipleFiles({ |
| 129 | + 'src/testing/type.ts': 'export type MyType = number;' |
| 130 | + }); |
| 131 | + |
| 132 | + // touch file to trigger build |
| 133 | + host.replaceInFile( |
| 134 | + 'src/app/app.component.ts', |
| 135 | + `'@angular/core';`, |
| 136 | + `'@angular/core';\n` |
| 137 | + ); |
| 138 | + break; |
| 139 | + |
| 140 | + case 2: |
| 141 | + // The second should have type.ts as unused |
| 142 | + expect(logger.includes(`type.ts ${warningMessageSuffix}`)).toBe(true, `Case ${buildNumber} failed.`); |
| 143 | + |
| 144 | + host.replaceInFile( |
| 145 | + 'src/app/app.component.ts', |
| 146 | + `'@angular/core';`, |
| 147 | + `'@angular/core';\nimport { MyType } from '../testing/type';` |
| 148 | + ); |
| 149 | + break; |
| 150 | + |
| 151 | + case 3: |
| 152 | + // The third should not have any unused files |
| 153 | + expect(logger.includes(warningMessageSuffix)).toBe(false, `Case ${buildNumber} failed.`); |
| 154 | + break; |
| 155 | + } |
| 156 | + |
| 157 | + logger.clear(); |
| 158 | + }), |
| 159 | + take(3), |
| 160 | + ) |
| 161 | + .toPromise(); |
| 162 | + await run.stop(); |
| 163 | + }); |
| 164 | + |
| 165 | +}); |
0 commit comments