Skip to content

Commit bf899ef

Browse files
devversionalxhub
authored andcommitted
build: prepare testing infrastructure for code splitting of core package (#60487)
When we switch to relative imports, shared `.d.ts` chunks can be generated. We need to also pull these into our mock virtual FS testing environments. Notably this does not cause a test slow-down because we are talking about very few extra `.d.ts` chunk files. In our experiments before, with no dts bundling, we saw test time increase from e.g. 20seconds to 100seconds. The 20s are still the same locally! In addition, since code for definitions can now reside in shared `.d.ts` chunks, the language service tests need to be adjusted in cases where they assert for code definition locations in `@angular/core`. A new helper prepares for more code to be moved into arbitrary `.d.ts` files; we should simply assert the definition comes out of `node_modules/@angular/core`. PR Close #60487
1 parent c13c53f commit bf899ef

4 files changed

Lines changed: 58 additions & 63 deletions

File tree

‎packages/compiler-cli/src/ngtsc/typecheck/testing/BUILD.bazel‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ ts_library(
2323
"//packages/compiler-cli/src/ngtsc/typecheck",
2424
"//packages/compiler-cli/src/ngtsc/typecheck/api",
2525
"//packages/compiler-cli/src/ngtsc/util",
26+
"@npm//tinyglobby",
2627
"@npm//typescript",
2728
],
2829
)

‎packages/compiler-cli/src/ngtsc/typecheck/testing/index.ts‎

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
import {readFileSync} from 'fs';
2525
import path from 'path';
2626
import ts from 'typescript';
27+
import {globSync} from 'tinyglobby';
2728

2829
import {
2930
absoluteFrom,
@@ -153,19 +154,19 @@ export function typescriptLibDts(): TestFile {
153154
};
154155
}
155156

157+
let _angularCoreDts: TestFile[] | null = null;
156158
export function angularCoreDtsFiles(): TestFile[] {
159+
if (_angularCoreDts !== null) {
160+
return _angularCoreDts;
161+
}
162+
157163
const directory = resolveFromRunfiles('angular/packages/core/npm_package');
164+
const dtsFiles = globSync('**/*.d.ts', {cwd: directory});
158165

159-
return [
160-
{
161-
name: absoluteFrom('/node_modules/@angular/core/index.d.ts'),
162-
contents: readFileSync(path.join(directory, 'index.d.ts'), 'utf8'),
163-
},
164-
{
165-
name: absoluteFrom('/node_modules/@angular/core/primitives/signals/index.d.ts'),
166-
contents: readFileSync(path.join(directory, 'primitives/signals/index.d.ts'), 'utf8'),
167-
},
168-
];
166+
return (_angularCoreDts = dtsFiles.map((fileName) => ({
167+
name: absoluteFrom(`/node_modules/@angular/core/${fileName}`),
168+
contents: readFileSync(path.join(directory, fileName), 'utf8'),
169+
})));
169170
}
170171

171172
export function angularAnimationsDts(): TestFile {
@@ -248,12 +249,7 @@ export function ngForDts(): TestFile {
248249

249250
export function ngForTypeCheckTarget(): TypeCheckingTarget {
250251
const dts = ngForDts();
251-
return {
252-
...dts,
253-
fileName: dts.name,
254-
source: dts.contents,
255-
templates: {},
256-
};
252+
return {...dts, fileName: dts.name, source: dts.contents, templates: {}};
257253
}
258254

259255
export const ALL_ENABLED_CONFIG: Readonly<TypeCheckingConfig> = {
@@ -427,9 +423,7 @@ export function tcb(
427423
checkTwoWayBoundEvents: true,
428424
...config,
429425
};
430-
options = options || {
431-
emitSpans: false,
432-
};
426+
options = options || {emitSpans: false};
433427

434428
const fileName = absoluteFrom('/type-check-file.ts');
435429

@@ -520,18 +514,12 @@ export function setup(
520514
}
521515
}
522516

523-
files.push({
524-
name: target.fileName,
525-
contents,
526-
});
517+
files.push({name: target.fileName, contents});
527518

528519
if (!target.fileName.endsWith('.d.ts')) {
529520
const shimName = TypeCheckShimGenerator.shimFor(target.fileName);
530521
shims.set(target.fileName, shimName);
531-
files.push({
532-
name: shimName,
533-
contents: 'export const MODULE = true;',
534-
});
522+
files.push({name: shimName, contents: 'export const MODULE = true;'});
535523
}
536524
}
537525

@@ -540,12 +528,7 @@ export function setup(
540528

541529
const {program, host, options} = makeProgram(
542530
files,
543-
{
544-
strictNullChecks: true,
545-
skipLibCheck: true,
546-
noImplicitAny: true,
547-
...opts,
548-
},
531+
{strictNullChecks: true, skipLibCheck: true, noImplicitAny: true, ...opts},
549532
/* host */ undefined,
550533
/* checkForErrors */ false,
551534
);
@@ -587,10 +570,7 @@ export function setup(
587570
if (shims.has(target.fileName)) {
588571
const shimFileName = shims.get(target.fileName)!;
589572
const shimSf = getSourceFileOrError(program, shimFileName);
590-
sfExtensionData(shimSf).fileShim = {
591-
extension: 'ngtypecheck',
592-
generatedFrom: target.fileName,
593-
};
573+
sfExtensionData(shimSf).fileShim = {extension: 'ngtypecheck', generatedFrom: target.fileName};
594574
}
595575

596576
for (const className of Object.keys(target.templates)) {
@@ -671,10 +651,7 @@ export function setup(
671651
if (!scopeMap.has(clazz)) {
672652
// This class wasn't part of the target set of components with templates, but is
673653
// probably a declaration used in one of them. Return an empty scope.
674-
const emptyScope: ScopeData = {
675-
dependencies: [],
676-
isPoisoned: false,
677-
};
654+
const emptyScope: ScopeData = {dependencies: [], isPoisoned: false};
678655
return {
679656
kind: ComponentScopeKind.NgModule,
680657
ngModule,
@@ -725,11 +702,7 @@ export function setup(
725702
typeCheckScopeRegistry,
726703
NOOP_PERF_RECORDER,
727704
);
728-
return {
729-
templateTypeChecker,
730-
program,
731-
programStrategy,
732-
};
705+
return {templateTypeChecker, program, programStrategy};
733706
}
734707

735708
/**
@@ -748,14 +721,7 @@ export function diagnose(
748721
const sfPath = absoluteFrom('/main.ts');
749722
const {program, templateTypeChecker} = setup(
750723
[
751-
{
752-
fileName: sfPath,
753-
templates: {
754-
'TestComponent': template,
755-
},
756-
source,
757-
declarations,
758-
},
724+
{fileName: sfPath, templates: {'TestComponent': template}, source, declarations},
759725
...additionalSources.map((testFile) => ({
760726
fileName: testFile.name,
761727
source: testFile.contents,
@@ -910,10 +876,7 @@ function getDirectiveMetaFromDeclaration(
910876
* Synthesize `ScopeData` metadata from an array of `TestDeclaration`s.
911877
*/
912878
function makeScope(program: ts.Program, sf: ts.SourceFile, decls: TestDeclaration[]): ScopeData {
913-
const scope: ScopeData = {
914-
dependencies: [],
915-
isPoisoned: false,
916-
};
879+
const scope: ScopeData = {dependencies: [], isPoisoned: false};
917880

918881
for (const decl of decls) {
919882
let declSf = sf;

‎packages/language-service/test/type_definitions_spec.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {initMockFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/te
1010

1111
import {
1212
assertFileNames,
13+
assertFilePaths,
1314
assertTextSpans,
1415
humanizeDocumentSpanLike,
1516
LanguageServiceTestEnv,
@@ -119,7 +120,7 @@ describe('type definitions', () => {
119120
expect(definitions!.length).toEqual(1);
120121

121122
assertTextSpans(definitions, ['OutputEmitterRef']);
122-
assertFileNames(definitions, ['index.d.ts']);
123+
assertFilePaths(definitions, [/node_modules\/@angular\/core\/.*\.d\.ts/]);
123124
});
124125
});
125126

@@ -158,7 +159,7 @@ describe('type definitions', () => {
158159
expect(definitions!.length).toEqual(1);
159160

160161
assertTextSpans(definitions, ['OutputRef']);
161-
assertFileNames(definitions, ['index.d.ts']);
162+
assertFilePaths(definitions, [/node_modules\/@angular\/core\/.*\.d\.ts/]);
162163
});
163164
});
164165

‎packages/language-service/testing/src/util.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,38 @@ export function assertFileNames(refs: Array<{fileName: string}>, expectedFileNam
2222
expect(new Set(actualFileNames)).toEqual(new Set(expectedFileNames));
2323
}
2424

25+
/**
26+
* Expect that a list of objects with a `fileName` property matches a set
27+
* of file paths.
28+
*
29+
* This assertion is independent of the order of either list.
30+
*/
31+
export function assertFilePaths(refs: Array<{fileName: string}>, expectedPaths: RegExp[]) {
32+
const actualPaths = Array.from(new Set(refs.map((r) => r.fileName)));
33+
34+
if (actualPaths.length !== expectedPaths.length) {
35+
expect(actualPaths.length)
36+
.withContext('Expected expected paths to be the same size.')
37+
.toBe(expectedPaths.length);
38+
return;
39+
}
40+
41+
for (const pattern of expectedPaths) {
42+
const matching = actualPaths.findIndex((p) => pattern.test(p));
43+
if (matching !== -1) {
44+
actualPaths.splice(matching, 1);
45+
} else {
46+
expect(true)
47+
.withContext(
48+
`Expected ${pattern} to match a file path. ` +
49+
`Remaining unmatched paths: ${actualPaths.join(', ')}`,
50+
)
51+
.toBe(false);
52+
return;
53+
}
54+
}
55+
}
56+
2557
export function assertTextSpans(items: Array<{textSpan: string}>, expectedTextSpans: string[]) {
2658
const actualSpans = items.map((item) => item.textSpan);
2759
expect(new Set(actualSpans)).toEqual(new Set(expectedTextSpans));
@@ -97,9 +129,7 @@ export function humanizeDocumentSpanLike<T extends ts.DocumentSpan>(
97129
: undefined,
98130
};
99131
}
100-
type Stringy<T> = {
101-
[P in keyof T]: string;
102-
};
132+
type Stringy<T> = {[P in keyof T]: string};
103133

104134
export function getText(contents: string, textSpan: ts.TextSpan) {
105135
return contents.slice(textSpan.start, textSpan.start + textSpan.length);

0 commit comments

Comments
 (0)