diff --git a/packages/language-service/test/diagnostics_spec.ts b/packages/language-service/test/diagnostics_spec.ts index 584c4ae205..347cf4b0d6 100644 --- a/packages/language-service/test/diagnostics_spec.ts +++ b/packages/language-service/test/diagnostics_spec.ts @@ -475,7 +475,7 @@ describe('diagnostics', () => { `Module '"../node_modules/@angular/core/core"' has no exported member 'OpaqueToken'.`); }); - describe('URL diagnostics', () => { + describe('templates', () => { it('should report errors for invalid templateUrls', () => { const fileName = mockHost.addCode(` @Component({ @@ -508,6 +508,53 @@ describe('diagnostics', () => { expect(urlDiagnostic).toBeUndefined(); }); + it('should report diagnostic for missing template or templateUrl', () => { + const fileName = mockHost.addCode(` + @Component({ + selector: 'app-example', + }) + export class AppExample {} + + @NgModule({ + declarations: [AppExample], + }) + export class AppModule {}`); + const diags = ngLS.getDiagnostics(fileName); + const missingTemplateError = diags.find( + d => d.messageText === `Component 'AppExample' must have a template or templateUrl`); + expect(missingTemplateError).toBeDefined(); + + const {start, length} = missingTemplateError !; + const content = mockHost.getFileContent(fileName) !; + expect(start).toBe(content.lastIndexOf('Component')); + expect(length).toBe('Component'.length); + }); + + it('should report diagnostic for both template and templateUrl', () => { + const fileName = mockHost.addCode(` + @Component({ + selector: 'app-example', + template: '
', + templateUrl: './example.html', + }) + export class AppExample {} + + @NgModule({ + declarations: [AppExample], + }) + export class AppModule {}`); + const diags = ngLS.getDiagnostics(fileName); + const dupTemplateError = diags.find( + d => d.messageText === + `Component 'AppExample' must not have both template and templateUrl`); + expect(dupTemplateError).toBeDefined(); + + const {start, length} = dupTemplateError !; + const content = mockHost.getFileContent(fileName) !; + expect(start).toBe(content.lastIndexOf('Component')); + expect(length).toBe('Component'.length); + }); + it('should report errors for invalid styleUrls', () => { const fileName = mockHost.addCode(` @Component({ diff --git a/packages/language-service/test/test_data.ts b/packages/language-service/test/test_data.ts index 6eee966eac..6b65ed9159 100644 --- a/packages/language-service/test/test_data.ts +++ b/packages/language-service/test/test_data.ts @@ -9,7 +9,7 @@ export const toh = { 'foo.ts': `export * from './app/app.component.ts';`, app: { - 'app.component.ts': `import { Component } from '@angular/core'; + 'app.component.ts': `import { Component, NgModule } from '@angular/core'; export class Hero { id: number;