test(compiler-cli): add extended template diagnostics end-to-end tests (#43107)

This commit adds extended template diagnostics end-to-end tests, to make
sure the diagnostics are generated correctly. Template checks are
already tested with unit tests.

Refs #42966

PR Close #43107
This commit is contained in:
Daniel Trevino
2021-08-06 20:55:01 +00:00
committed by Alex Rickabaugh
parent ff858ac9d8
commit 4d42a3ab10
@@ -0,0 +1,107 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as ts from 'typescript';
import {ErrorCode} from '../../src/ngtsc/diagnostics';
import {runInEachFileSystem} from '../../src/ngtsc/file_system/testing';
import {getSourceCodeForDiagnostic, loadStandardTestFiles} from '../../src/ngtsc/testing';
import {NgtscTestEnvironment} from './env';
const testFiles = loadStandardTestFiles({fakeCore: true, fakeCommon: true});
runInEachFileSystem(() => {
describe('ngtsc extended template checks', () => {
let env!: NgtscTestEnvironment;
beforeEach(() => {
env = NgtscTestEnvironment.setup(testFiles);
env.tsconfig({_extendedTemplateDiagnostics: true, strictTemplates: true});
});
it('should produce invalid banana in box warning', () => {
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test',
template: '<div ([notARealThing])="bar"></div>',
})
class TestCmp {
bar: string = "text";
}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
expect(diags[0].code).toBe(ErrorCode.INVALID_BANANA_IN_BOX);
expect(getSourceCodeForDiagnostic(diags[0])).toBe('([notARealThing])="bar"');
});
it('should produce invalid banana in box warning with external html file', () => {
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test',
templateUrl: './test.html',
})
class TestCmp {
bar: string = "text";
}
`);
env.write('test.html', `
<div ([notARealThing])="bar"></div>
`);
const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
expect(diags[0].code).toBe(ErrorCode.INVALID_BANANA_IN_BOX);
expect(getSourceCodeForDiagnostic(diags[0])).toBe('([notARealThing])="bar"');
});
it('should not produce extended diagnostics if flag is disabled', () => {
env.tsconfig({_extendedTemplateDiagnostics: false});
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test',
template: '<div ([notARealThing])="bar"></div>',
})
class TestCmp {
bar: string = "text";
}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toBe(0);
});
it('should throw error if _extendedTemplateDiagnostics option is enabled and strictTemplates disabled',
() => {
env.tsconfig({_extendedTemplateDiagnostics: true, strictTemplates: false});
env.write('test.ts', `
import {Component} from '@angular/core';
@Component({
selector: 'test',
template: '<div ([notARealThing])="bar"></div>',
})
class TestCmp {
bar: string = "text";
}
`);
const diags = env.driveDiagnostics();
expect(diags.length).toBe(1);
expect(diags[0].messageText)
.toMatch(
/Error: The '_extendedTemplateDiagnostics' option requires 'strictTemplates' to also be enabled./);
});
});
});