Skip to content

Commit 95cfd0b

Browse files
danieltre23thePunderWoman
authored andcommitted
test(compiler-cli): add tests for NullishCoalescingNotNullableCheck (angular#43232)
Add tests to make sure the nullish coalescing check is generating the correct diagnostics. Refs angular#42966 PR Close angular#43232
1 parent 0802b59 commit 95cfd0b

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("//tools:defaults.bzl", "jasmine_node_test", "ts_library")
2+
3+
ts_library(
4+
name = "test_lib",
5+
testonly = True,
6+
srcs = ["spec.ts"],
7+
deps = [
8+
"//packages/compiler",
9+
"//packages/compiler-cli/src/ngtsc/diagnostics",
10+
"//packages/compiler-cli/src/ngtsc/file_system",
11+
"//packages/compiler-cli/src/ngtsc/file_system/testing",
12+
"//packages/compiler-cli/src/ngtsc/testing",
13+
"//packages/compiler-cli/src/ngtsc/typecheck/extended",
14+
"//packages/compiler-cli/src/ngtsc/typecheck/extended/checks/nullish_coalescing_not_nullable",
15+
"//packages/compiler-cli/src/ngtsc/typecheck/testing",
16+
"@npm//typescript",
17+
],
18+
)
19+
20+
jasmine_node_test(
21+
name = "test",
22+
bootstrap = ["//tools/testing:node_no_angular_es5"],
23+
deps = [
24+
":test_lib",
25+
],
26+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import * as ts from 'typescript';
10+
11+
import {ErrorCode, ngErrorCode} from '../../../../../diagnostics';
12+
import {absoluteFrom, getSourceFileOrError} from '../../../../../file_system';
13+
import {runInEachFileSystem} from '../../../../../file_system/testing';
14+
import {getSourceCodeForDiagnostic} from '../../../../../testing';
15+
import {getClass, setup} from '../../../../testing';
16+
import {NullishCoalescingNotNullableCheck} from '../../../checks/nullish_coalescing_not_nullable';
17+
import {ExtendedTemplateCheckerImpl} from '../../../src/extended_template_checker';
18+
19+
runInEachFileSystem(() => {
20+
describe('NullishCoalescingNotNullableCheck', () => {
21+
it('should produce nullish coalescing warning', () => {
22+
const fileName = absoluteFrom('/main.ts');
23+
const {program, templateTypeChecker} = setup([{
24+
fileName,
25+
templates: {
26+
'TestCmp': `{{ var1 ?? 'foo' }}`,
27+
},
28+
source: 'export class TestCmp { var1: string = "text"; }'
29+
}]);
30+
const sf = getSourceFileOrError(program, fileName);
31+
const component = getClass(sf, 'TestCmp');
32+
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
33+
templateTypeChecker, program.getTypeChecker(), [new NullishCoalescingNotNullableCheck()]);
34+
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
35+
expect(diags.length).toBe(1);
36+
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
37+
expect(diags[0].code).toBe(ngErrorCode(ErrorCode.NULLISH_COALESCING_NOT_NULLABLE));
38+
expect(getSourceCodeForDiagnostic(diags[0])).toBe(`var1 ?? 'foo'`);
39+
});
40+
41+
it('should not produce nullish coalescing warning for a nullable type', () => {
42+
const fileName = absoluteFrom('/main.ts');
43+
const {program, templateTypeChecker} = setup([{
44+
fileName,
45+
templates: {
46+
'TestCmp': `{{ var1 ?? 'foo' }}`,
47+
},
48+
source: 'export class TestCmp { var1: string | null = "text"; }'
49+
}]);
50+
const sf = getSourceFileOrError(program, fileName);
51+
const component = getClass(sf, 'TestCmp');
52+
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
53+
templateTypeChecker, program.getTypeChecker(), [new NullishCoalescingNotNullableCheck()]);
54+
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
55+
expect(diags.length).toBe(0);
56+
});
57+
58+
it('should not produce nullish coalescing warning for a type that includes undefined', () => {
59+
const fileName = absoluteFrom('/main.ts');
60+
const {program, templateTypeChecker} = setup([{
61+
fileName,
62+
templates: {
63+
'TestCmp': `{{ var1 ?? 'foo' }}`,
64+
},
65+
source: 'export class TestCmp { var1: string | undefined = "text"; }'
66+
}]);
67+
const sf = getSourceFileOrError(program, fileName);
68+
const component = getClass(sf, 'TestCmp');
69+
const extendedTemplateChecker = new ExtendedTemplateCheckerImpl(
70+
templateTypeChecker, program.getTypeChecker(), [new NullishCoalescingNotNullableCheck()]);
71+
const diags = extendedTemplateChecker.getDiagnosticsForComponent(component);
72+
expect(diags.length).toBe(0);
73+
});
74+
});
75+
});

packages/compiler-cli/test/ngtsc/extended_template_diagnostics_spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,43 @@ runInEachFileSystem(() => {
103103
.toMatch(
104104
/Error: The '_extendedTemplateDiagnostics' option requires 'strictTemplates' to also be enabled./);
105105
});
106+
107+
it(`should produce nullish coalescing not nullable warning`, () => {
108+
env.write('test.ts', `
109+
import {Component} from '@angular/core';
110+
@Component({
111+
selector: 'test',
112+
template: '{{ bar ?? "foo" }}',
113+
})
114+
export class TestCmp {
115+
bar: string = "text";
116+
}
117+
`);
118+
119+
const diags = env.driveDiagnostics();
120+
expect(diags.length).toBe(1);
121+
expect(diags[0].category).toBe(ts.DiagnosticCategory.Warning);
122+
expect(diags[0].code).toBe(ngErrorCode(ErrorCode.NULLISH_COALESCING_NOT_NULLABLE));
123+
expect(getSourceCodeForDiagnostic(diags[0])).toBe('bar ?? "foo"');
124+
});
125+
126+
it(`should not produce nullish coalescing not nullable warning with strictNullChecks disabled`,
127+
() => {
128+
env.tsconfig(
129+
{_extendedTemplateDiagnostics: true, strictTemplates: true, strictNullChecks: false});
130+
env.write('test.ts', `
131+
import {Component} from '@angular/core';
132+
@Component({
133+
selector: 'test',
134+
template: '{{ bar ?? "foo" }}',
135+
})
136+
export class TestCmp {
137+
bar: string = undefined;
138+
}
139+
`);
140+
141+
const diags = env.driveDiagnostics();
142+
expect(diags.length).toBe(0);
143+
});
106144
});
107145
});

0 commit comments

Comments
 (0)