refactor(core): static-query migration should not fail for test files (#30034)

Currently when someone runs `ng update` with the static-query migration,
the migration can fail with an error saying that the `AOT` compiler could not
be created. This can happen if the CLI project contains a test `tsconfig.json`
that is picked up by the schematic.

Due to the fact that spec tsconfig files cannot be ran with NGC (e.g. test
components are not part of a module; not all source files are guaranteed to
be included), test `tsconfig` projects will now use a new `test` migration
strategy where all queries within tests are left untouched and a TODO is added.

PR Close #30034
This commit is contained in:
Paul Gschwendtner
2019-04-22 21:11:29 +02:00
committed by Ben Lesh
parent 00ce9aab5b
commit 364250e7a6
12 changed files with 190 additions and 50 deletions

View File

@ -29,6 +29,9 @@ describe('move-document migration', () => {
lib: ['es2015'],
}
}));
writeFile('/angular.json', JSON.stringify({
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
previousWorkingDir = shx.pwd();
tmpDirPath = getSystemPath(host.root);

View File

@ -22,7 +22,16 @@ describe('project tsconfig paths', () => {
{my_name: {architect: {build: {options: {tsConfig: './my-custom-config.json'}}}}}
}));
expect(getProjectTsConfigPaths(testTree)).toEqual(['my-custom-config.json']);
expect(getProjectTsConfigPaths(testTree).buildPaths).toEqual(['my-custom-config.json']);
});
it('should detect test tsconfig path inside of angular.json file', () => {
testTree.create('/my-test-config.json', '');
testTree.create('/angular.json', JSON.stringify({
projects: {my_name: {architect: {test: {options: {tsConfig: './my-test-config.json'}}}}}
}));
expect(getProjectTsConfigPaths(testTree).testPaths).toEqual(['my-test-config.json']);
});
it('should detect test tsconfig path inside of .angular.json file', () => {
@ -32,25 +41,23 @@ describe('project tsconfig paths', () => {
{with_tests: {architect: {test: {options: {tsConfig: './my-test-config.json'}}}}}
}));
expect(getProjectTsConfigPaths(testTree)).toEqual(['my-test-config.json']);
expect(getProjectTsConfigPaths(testTree).testPaths).toEqual(['my-test-config.json']);
});
it('should detect common tsconfigs if no workspace config could be found', () => {
testTree.create('/tsconfig.json', '');
testTree.create('/src/tsconfig.json', '');
testTree.create('/src/tsconfig.app.json', '');
testTree.create('/src/tsconfig.spec.json', '');
expect(getProjectTsConfigPaths(testTree)).toEqual([
'tsconfig.json', 'src/tsconfig.json', 'src/tsconfig.app.json'
]);
expect(getProjectTsConfigPaths(testTree).buildPaths).toEqual(['src/tsconfig.app.json']);
expect(getProjectTsConfigPaths(testTree).testPaths).toEqual(['src/tsconfig.spec.json']);
});
it('should not return duplicate tsconfig files', () => {
testTree.create('/tsconfig.json', '');
testTree.create('/.angular.json', JSON.stringify({
projects: {app: {architect: {test: {options: {tsConfig: 'tsconfig.json'}}}}}
projects: {app: {architect: {build: {options: {tsConfig: 'tsconfig.json'}}}}}
}));
expect(getProjectTsConfigPaths(testTree)).toEqual(['tsconfig.json']);
expect(getProjectTsConfigPaths(testTree).buildPaths).toEqual(['tsconfig.json']);
});
});

View File

@ -31,6 +31,9 @@ describe('static-queries migration with template strategy', () => {
lib: ['es2015'],
}
}));
writeFile('/angular.json', JSON.stringify({
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
warnOutput = [];
runner.logger.subscribe(logEntry => {
@ -515,5 +518,54 @@ describe('static-queries migration with template strategy', () => {
expect(console.error).toHaveBeenCalledTimes(0);
});
it('should always use the test migration strategy for test tsconfig files', async() => {
writeFile('/src/tsconfig.spec.json', JSON.stringify({
compilerOptions: {
experimentalDecorators: true,
lib: ['es2015'],
},
files: [
'test.ts',
],
}));
writeFile('/src/test.ts', `
import {ViewChild} from '@angular/core';
import {AppComponent} from './app.component';
@Component({template: '<span #test>Test</span>'})
class MyTestComponent {
@ViewChild('test') query: any;
}
`);
writeFile('/src/app.component.ts', `
import {Component, ViewChild} from '@angular/core';
@Component({template: '<span #test></span>'})
export class AppComponent {
@ViewChild('test') query: any;
}
`);
writeFile('/src/app.module.ts', `
import {NgModule} from '@angular/core';
import {AppComponent} from './app.component';
@NgModule({declarations: [AppComponent]})
export class MyModule {}
`);
spyOn(console, 'error').and.callThrough();
await runMigration();
expect(console.error).toHaveBeenCalledTimes(0);
expect(tree.readContent('/src/test.ts'))
.toContain(`@ViewChild('test', /* TODO: add static flag */ {}) query: any;`);
expect(tree.readContent('/src/app.component.ts'))
.toContain(`@ViewChild('test', { static: true }) query: any;`);
});
});
});

View File

@ -35,6 +35,9 @@ describe('static-queries migration with usage strategy', () => {
lib: ['es2015'],
}
}));
writeFile('/angular.json', JSON.stringify({
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
previousWorkingDir = shx.pwd();
tmpDirPath = getSystemPath(host.root);
@ -598,6 +601,9 @@ describe('static-queries migration with usage strategy', () => {
// Move the tsconfig into a subdirectory. This ensures that the update is properly
// recorded for TypeScript projects not at the schematic tree root.
host.sync.rename(normalize('/tsconfig.json'), normalize('/src/tsconfig.json'));
writeFile('/angular.json', JSON.stringify({
projects: {t: {architect: {build: {options: {tsConfig: './src/tsconfig.json'}}}}}
}));
await runMigration();

View File

@ -30,6 +30,9 @@ describe('template variable assignment migration', () => {
lib: ['es2015'],
}
}));
writeFile('/angular.json', JSON.stringify({
projects: {t: {architect: {build: {options: {tsConfig: './tsconfig.json'}}}}}
}));
warnOutput = [];
runner.logger.subscribe(logEntry => {