mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
test(compiler-cli): add tests for NullishCoalescingNotNullableCheck (#43232)
Add tests to make sure the nullish coalescing check is generating the correct diagnostics. Refs #42966 PR Close #43232
This commit is contained in:
committed by
Jessica Janiuk
parent
0802b59a7b
commit
95cfd0b0e1
@@ -103,5 +103,43 @@ runInEachFileSystem(() => {
|
||||
.toMatch(
|
||||
/Error: The '_extendedTemplateDiagnostics' option requires 'strictTemplates' to also be enabled./);
|
||||
});
|
||||
|
||||
it(`should produce nullish coalescing not nullable warning`, () => {
|
||||
env.write('test.ts', `
|
||||
import {Component} from '@angular/core';
|
||||
@Component({
|
||||
selector: 'test',
|
||||
template: '{{ bar ?? "foo" }}',
|
||||
})
|
||||
export 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(ngErrorCode(ErrorCode.NULLISH_COALESCING_NOT_NULLABLE));
|
||||
expect(getSourceCodeForDiagnostic(diags[0])).toBe('bar ?? "foo"');
|
||||
});
|
||||
|
||||
it(`should not produce nullish coalescing not nullable warning with strictNullChecks disabled`,
|
||||
() => {
|
||||
env.tsconfig(
|
||||
{_extendedTemplateDiagnostics: true, strictTemplates: true, strictNullChecks: false});
|
||||
env.write('test.ts', `
|
||||
import {Component} from '@angular/core';
|
||||
@Component({
|
||||
selector: 'test',
|
||||
template: '{{ bar ?? "foo" }}',
|
||||
})
|
||||
export class TestCmp {
|
||||
bar: string = undefined;
|
||||
}
|
||||
`);
|
||||
|
||||
const diags = env.driveDiagnostics();
|
||||
expect(diags.length).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user