Skip to content

Commit 047ca3e

Browse files
committed
test(compiler): add integration tests, refine unit tests
1 parent 5f1028b commit 047ca3e

File tree

5 files changed

+168
-64
lines changed

5 files changed

+168
-64
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
template: 'Component Export',
5+
styles: [
6+
`
7+
p {
8+
color: #000;
9+
}
10+
`,
11+
],
12+
})
13+
export class ComponentExport {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const num42 = 42;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2022",
4+
"module": "NodeNext",
5+
"moduleResolution": "NodeNext",
6+
"files": [
7+
"packages/compiler/mocks/fixtures/integration/tsconfig-variations/const-export.ts",
8+
"packages/compiler/mocks/fixtures/integration/tsconfig-variations/component-export.ts"
9+
]
10+
}
11+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import { beforeEach, describe, expect, it } from 'vitest';
2+
import { buildAndAnalyzeWithParallelCompilation } from './build-and-analyze'; // Adjust path accordingly
3+
import { ParallelCompilation } from '@angular/build/src/tools/angular/compilation/parallel-compilation';
4+
import { JavaScriptTransformer } from '@angular/build/src/tools/esbuild/javascript-transformer';
5+
import * as path from 'node:path';
6+
7+
describe('buildAndAnalyzeWithParallelCompilation execution', () => {
8+
let parallelCompilation: ParallelCompilation;
9+
let javascriptTransformer: JavaScriptTransformer;
10+
let typescriptFileCache: Map<string, string | Uint8Array>;
11+
12+
beforeEach(async () => {
13+
typescriptFileCache = new Map();
14+
15+
parallelCompilation = new ParallelCompilation(false, false);
16+
const tsConfigPath =
17+
'./mocks/fixtures/integration/tsconfig-variations/tsconfig.minimal.json';
18+
19+
await parallelCompilation.initialize(tsConfigPath, {
20+
transformStylesheet(_: string, __: string): Promise<string | null> {
21+
return Promise.resolve('MOCK_TRANSFORMED_STYLESHEET');
22+
},
23+
processWebWorker: (_: string, __: string) => 'PROCESS_WEB_WORKER',
24+
});
25+
26+
javascriptTransformer = new JavaScriptTransformer(
27+
{
28+
jit: false,
29+
sourcemap: false,
30+
},
31+
3
32+
);
33+
});
34+
35+
it('should compile and cache transformed TypeScript files', async () => {
36+
const transformDataSpy = vi.spyOn(javascriptTransformer, 'transformData');
37+
await expect(
38+
buildAndAnalyzeWithParallelCompilation(
39+
parallelCompilation,
40+
typescriptFileCache,
41+
javascriptTransformer
42+
)
43+
).resolves.not.toThrow();
44+
expect(typescriptFileCache.size).toBe(2);
45+
expect(transformDataSpy).toHaveBeenCalledTimes(2);
46+
47+
const constTransformedContent = typescriptFileCache.get(
48+
path.join(
49+
process.cwd(),
50+
'mocks/fixtures/integration/tsconfig-variations/const-export.ts'
51+
)
52+
);
53+
expect(constTransformedContent).toMatchInlineSnapshot(`
54+
""use strict";
55+
Object.defineProperty(exports, "__esModule", { value: true });
56+
exports.num42 = void 0;
57+
exports.num42 = 42;
58+
"
59+
`);
60+
const compTransformedContent = typescriptFileCache.get(
61+
path.join(
62+
process.cwd(),
63+
'mocks/fixtures/integration/tsconfig-variations/component-export.ts'
64+
)
65+
);
66+
expect(compTransformedContent).toMatchInlineSnapshot(`
67+
""use strict";
68+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
69+
if (k2 === undefined) k2 = k;
70+
var desc = Object.getOwnPropertyDescriptor(m, k);
71+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
72+
desc = { enumerable: true, get: function() { return m[k]; } };
73+
}
74+
Object.defineProperty(o, k2, desc);
75+
}) : (function(o, m, k, k2) {
76+
if (k2 === undefined) k2 = k;
77+
o[k2] = m[k];
78+
}));
79+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
80+
Object.defineProperty(o, "default", { enumerable: true, value: v });
81+
}) : function(o, v) {
82+
o["default"] = v;
83+
});
84+
var __importStar = (this && this.__importStar) || function (mod) {
85+
if (mod && mod.__esModule) return mod;
86+
var result = {};
87+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
88+
__setModuleDefault(result, mod);
89+
return result;
90+
};
91+
Object.defineProperty(exports, "__esModule", { value: true });
92+
exports.ComponentExport = void 0;
93+
const core_1 = require("@angular/core");
94+
const i0 = __importStar(require("@angular/core"));
95+
class ComponentExport {
96+
static ɵfac = function ComponentExport_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || ComponentExport)(); };
97+
static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: ComponentExport, selectors: [["ng-component"]], decls: 1, vars: 0, template: function ComponentExport_Template(rf, ctx) { if (rf & 1) {
98+
i0.ɵɵtext(0, "Component Export");
99+
} }, styles: ["MOCK_TRANSFORMED_STYLESHEET"] });
100+
}
101+
exports.ComponentExport = ComponentExport;
102+
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassDebugInfo(ComponentExport, { className: "ComponentExport", filePath: "mocks/fixtures/integration/tsconfig-variations/component-export.ts", lineNumber: 7 }); })();
103+
"
104+
`);
105+
});
106+
});
Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,36 @@
11
import { describe, it, expect, vi, beforeEach } from 'vitest';
2-
import { buildAndAnalyzeWithParallelCompilation } from './build-and-analyze'; // Adjust path accordingly
2+
import { buildAndAnalyzeWithParallelCompilation } from './build-and-analyze';
33
import { ParallelCompilation } from '@angular/build/src/tools/angular/compilation/parallel-compilation';
44
import { JavaScriptTransformer } from '@angular/build/src/tools/esbuild/javascript-transformer';
55

66
describe('buildAndAnalyzeWithParallelCompilation', () => {
7+
let parallelCompilation: ParallelCompilation;
8+
let javascriptTransformer: JavaScriptTransformer;
9+
let typescriptFileCache: Map<string, string | Uint8Array>;
710
const mockEmitAffectedFiles = vi.fn();
8-
911
const mockTransformData = vi.fn();
1012

1113
beforeEach(() => {
1214
vi.clearAllMocks();
13-
mockTransformData.mockImplementation((_file, content) =>
14-
Promise.resolve(`transformed(${content})`)
15+
16+
mockTransformData.mockImplementation((_file, _) =>
17+
Promise.resolve('MOCK_TRANSFORMED')
1518
);
16-
mockEmitAffectedFiles.mockResolvedValue([
17-
{ filename: 'C:/path/to/file.ts', contents: 'console.log("Hello");' },
18-
{ filename: '/home/user/project/file2.ts', contents: 'const a = 42;' },
19-
]);
20-
});
2119

22-
it('should call emitAffectedFiles and iterate over result of emitted files', async () => {
23-
const parallelCompilation = {
20+
parallelCompilation = {
2421
emitAffectedFiles: mockEmitAffectedFiles,
2522
} as unknown as ParallelCompilation;
26-
const javascriptTransformer = {
23+
javascriptTransformer = {
2724
transformData: mockTransformData,
2825
} as unknown as JavaScriptTransformer;
29-
const typescriptFileCache = new Map<string, string | Uint8Array>();
26+
typescriptFileCache = new Map<string, string | Uint8Array>();
27+
});
28+
29+
it('should call emitAffectedFiles and iterate over result of emitted files', async () => {
30+
mockEmitAffectedFiles.mockResolvedValue([
31+
{ filename: 'file1.ts', contents: '' },
32+
{ filename: 'file2.ts', contents: '' },
33+
]);
3034

3135
await expect(
3236
buildAndAnalyzeWithParallelCompilation(
@@ -36,16 +40,13 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
3640
)
3741
).resolves.not.toThrow();
3842
expect(mockEmitAffectedFiles).toHaveBeenCalledTimes(1);
43+
expect(mockTransformData).toHaveBeenCalledTimes(2);
3944
});
4045

41-
it('should call transformData for each file emitted by emitAffectedFiles', async () => {
42-
const parallelCompilation = {
43-
emitAffectedFiles: mockEmitAffectedFiles,
44-
} as unknown as ParallelCompilation;
45-
const javascriptTransformer = {
46-
transformData: mockTransformData,
47-
} as unknown as JavaScriptTransformer;
48-
const typescriptFileCache = new Map<string, string | Uint8Array>();
46+
it('should normalize file names before emitting', async () => {
47+
mockEmitAffectedFiles.mockResolvedValue([
48+
{ filename: 'C:/file.ts', contents: '' },
49+
]);
4950

5051
await expect(
5152
buildAndAnalyzeWithParallelCompilation(
@@ -54,32 +55,20 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
5455
javascriptTransformer
5556
)
5657
).resolves.not.toThrow();
57-
expect(mockTransformData).toHaveBeenCalledTimes(2);
58+
5859
expect(mockTransformData).toHaveBeenNthCalledWith(
5960
1,
60-
'/path/to/file.ts',
61-
'console.log("Hello");',
62-
true,
63-
false
64-
);
65-
expect(mockTransformData).toHaveBeenNthCalledWith(
66-
2,
67-
'/home/user/project/file2.ts',
68-
'const a = 42;',
61+
'/file.ts',
62+
'',
6963
true,
7064
false
7165
);
7266
});
7367

7468
it('should add emitted files to cache', async () => {
75-
const parallelCompilation = {
76-
emitAffectedFiles: mockEmitAffectedFiles,
77-
} as unknown as ParallelCompilation;
78-
const javascriptTransformer = {
79-
transformData: mockTransformData,
80-
} as unknown as JavaScriptTransformer;
81-
const typescriptFileCache = new Map<string, string | Uint8Array>();
82-
69+
mockEmitAffectedFiles.mockResolvedValue([
70+
{ filename: 'file.ts', contents: '' },
71+
]);
8372
await expect(
8473
buildAndAnalyzeWithParallelCompilation(
8574
parallelCompilation,
@@ -88,20 +77,15 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
8877
)
8978
).resolves.not.toThrow();
9079

91-
expect(typescriptFileCache.size).toBe(2);
92-
expect(typescriptFileCache.has('/path/to/file.ts')).toBe(true);
93-
expect(typescriptFileCache.has('/home/user/project/file2.ts')).toBe(true);
80+
expect(typescriptFileCache.size).toBe(1);
81+
expect(typescriptFileCache.has('file.ts')).toBe(true);
82+
expect(typescriptFileCache.get('file.ts')).toEqual(expect.any(String));
9483
});
9584

96-
it('should transform and cache emitted TypeScript files', async () => {
97-
const parallelCompilation = {
98-
emitAffectedFiles: mockEmitAffectedFiles,
99-
} as unknown as ParallelCompilation;
100-
const javascriptTransformer = {
101-
transformData: mockTransformData,
102-
} as unknown as JavaScriptTransformer;
103-
const typescriptFileCache = new Map<string, string | Uint8Array>();
104-
85+
it('should transform the file content', async () => {
86+
mockEmitAffectedFiles.mockResolvedValue([
87+
{ filename: 'file.ts', contents: '' },
88+
]);
10589
await expect(
10690
buildAndAnalyzeWithParallelCompilation(
10791
parallelCompilation,
@@ -110,18 +94,7 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
11094
)
11195
).resolves.not.toThrow();
11296

113-
expect(mockEmitAffectedFiles).toHaveBeenCalledTimes(1);
114-
expect(mockTransformData).toHaveBeenCalledTimes(2);
115-
expect(typescriptFileCache.size).toBe(2);
116-
117-
expect(typescriptFileCache.has('/path/to/file.ts')).toBe(true);
118-
expect(typescriptFileCache.has('/home/user/project/file2.ts')).toBe(true);
119-
120-
expect(typescriptFileCache.get('/path/to/file.ts')).toBe(
121-
'transformed(console.log("Hello");)'
122-
);
123-
expect(typescriptFileCache.get('/home/user/project/file2.ts')).toBe(
124-
'transformed(const a = 42;)'
125-
);
97+
expect(mockTransformData).toHaveBeenCalledTimes(1);
98+
expect(typescriptFileCache.get('file.ts')).toBe('MOCK_TRANSFORMED');
12699
});
127100
});

0 commit comments

Comments
 (0)