Skip to content

Commit 5e6b42c

Browse files
Alanhansl
Alan
authored andcommitted
test: add tests for elide imports
This also adds the option to provide addition files when using `createTypescriptContext` this is paramount for the elide imports tests as without this certain symbols won't have the full details. Which will cause tests to be false positive and re-surface issues like #13212
1 parent bdef022 commit 5e6b42c

File tree

3 files changed

+271
-110
lines changed

3 files changed

+271
-110
lines changed

packages/angular_devkit/build_angular/test/browser/aot_spec_large.ts

-109
Original file line numberDiff line numberDiff line change
@@ -30,113 +30,4 @@ describe('Browser Builder AOT', () => {
3030
}),
3131
).toPromise().then(done, done.fail);
3232
});
33-
34-
it('works with aliased imports', (done) => {
35-
const overrides = { aot: true };
36-
37-
host.writeMultipleFiles({
38-
'src/app/app.component.ts': `import { Component } from '@angular/core';
39-
import { from as fromPromise } from 'rxjs';
40-
41-
@Component({
42-
selector: 'app-root',
43-
templateUrl: './app.component.html',
44-
styleUrls: ['./app.component.css']
45-
})
46-
export class AppComponent {
47-
title = 'app-component';
48-
49-
constructor() {
50-
console.log(fromPromise(Promise.resolve('test')));
51-
}
52-
}`,
53-
});
54-
55-
runTargetSpec(host, browserTargetSpec, overrides).pipe(
56-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
57-
tap(() => {
58-
const fileName = join(outputPath, 'main.js');
59-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
60-
// if the aliased import was dropped this won't be rewired to a webpack module.
61-
expect(content).toMatch(/rxjs__WEBPACK_IMPORTED_.+[\"from\"]/);
62-
expect(content).not.toContain('fromPromise');
63-
}),
64-
).toPromise().then(done, done.fail);
65-
});
66-
67-
it('works with aliased imports from an exported object literal', (done) => {
68-
const overrides = { aot: true };
69-
70-
host.writeMultipleFiles({
71-
'src/foo.ts': `
72-
import { from as fromPromise } from 'rxjs';
73-
export { fromPromise };
74-
`,
75-
'src/app/app.component.ts': `
76-
import { Component } from '@angular/core';
77-
import { fromPromise } from '../foo';
78-
79-
@Component({
80-
selector: 'app-root',
81-
templateUrl: './app.component.html',
82-
styleUrls: ['./app.component.css']
83-
})
84-
export class AppComponent {
85-
title = 'app-component';
86-
87-
constructor() {
88-
console.log(fromPromise(Promise.resolve('test')));
89-
}
90-
}`,
91-
});
92-
93-
runTargetSpec(host, browserTargetSpec, overrides).pipe(
94-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
95-
tap(() => {
96-
const fileName = join(outputPath, 'main.js');
97-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
98-
// if the aliased import was dropped this won't be rewired to a webpack module.
99-
expect(content).toMatch(/rxjs__WEBPACK_IMPORTED_.+[\"from\"]/);
100-
expect(content).toMatch(/rxjs__WEBPACK_IMPORTED_.+[\"fromPromise\"]/);
101-
}),
102-
).toPromise().then(done, done.fail);
103-
});
104-
105-
it('works with aliased imports from an alias export', (done) => {
106-
const overrides = { aot: true };
107-
108-
host.writeMultipleFiles({
109-
'src/foo.ts': `
110-
export { from as fromPromise } from 'rxjs';
111-
`,
112-
'src/app/app.component.ts': `
113-
import { Component } from '@angular/core';
114-
import { fromPromise } from '../foo';
115-
116-
@Component({
117-
selector: 'app-root',
118-
templateUrl: './app.component.html',
119-
styleUrls: ['./app.component.css']
120-
})
121-
export class AppComponent {
122-
title = 'app-component';
123-
124-
constructor() {
125-
console.log(fromPromise(Promise.resolve('test')));
126-
}
127-
}`,
128-
});
129-
130-
runTargetSpec(host, browserTargetSpec, overrides).pipe(
131-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
132-
tap(() => {
133-
const fileName = join(outputPath, 'main.js');
134-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
135-
// if the aliased import was dropped this won't be rewired to a webpack module.
136-
expect(content).toMatch(/rxjs__WEBPACK_IMPORTED_.+[\"from\"]/);
137-
expect(content).toMatch(/rxjs__WEBPACK_IMPORTED_.+[\"fromPromise\"]/);
138-
}),
139-
).toPromise().then(done, done.fail);
140-
});
141-
14233
});

packages/ngtools/webpack/src/transformers/ast_helpers.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ export function getLastNode(sourceFile: ts.SourceFile): ts.Node | null {
4545
const basePath = '/project/src/';
4646
const fileName = basePath + 'test-file.ts';
4747

48-
export function createTypescriptContext(content: string) {
48+
export function createTypescriptContext(content: string, additionalFiles?: Record<string, string>) {
4949
// Set compiler options.
5050
const compilerOptions: ts.CompilerOptions = {
5151
noEmitOnError: false,
5252
allowJs: true,
5353
newLine: ts.NewLineKind.LineFeed,
54+
moduleResolution: ts.ModuleResolutionKind.NodeJs,
5455
target: ts.ScriptTarget.ESNext,
5556
skipLibCheck: true,
5657
sourceMap: false,
@@ -68,6 +69,12 @@ export function createTypescriptContext(content: string) {
6869
// Add a dummy file to host content.
6970
compilerHost.writeFile(fileName, content, false);
7071

72+
if (additionalFiles) {
73+
for (const key in additionalFiles) {
74+
compilerHost.writeFile(basePath + key, additionalFiles[key], false);
75+
}
76+
}
77+
7178
// Create the TypeScript program.
7279
const program = ts.createProgram([fileName], compilerOptions, compilerHost);
7380

0 commit comments

Comments
 (0)