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:
Daniel Trevino
2021-08-13 19:47:41 +00:00
committed by Jessica Janiuk
parent 0802b59a7b
commit 95cfd0b0e1
3 changed files with 139 additions and 0 deletions
@@ -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);
});
});
});