1
1
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' ;
3
3
import { ParallelCompilation } from '@angular/build/src/tools/angular/compilation/parallel-compilation' ;
4
4
import { JavaScriptTransformer } from '@angular/build/src/tools/esbuild/javascript-transformer' ;
5
5
6
6
describe ( 'buildAndAnalyzeWithParallelCompilation' , ( ) => {
7
+ let parallelCompilation : ParallelCompilation ;
8
+ let javascriptTransformer : JavaScriptTransformer ;
9
+ let typescriptFileCache : Map < string , string | Uint8Array > ;
7
10
const mockEmitAffectedFiles = vi . fn ( ) ;
8
-
9
11
const mockTransformData = vi . fn ( ) ;
10
12
11
13
beforeEach ( ( ) => {
12
14
vi . clearAllMocks ( ) ;
13
- mockTransformData . mockImplementation ( ( _file , content ) =>
14
- Promise . resolve ( `transformed(${ content } )` )
15
+
16
+ mockTransformData . mockImplementation ( ( _file , _ ) =>
17
+ Promise . resolve ( 'MOCK_TRANSFORMED' )
15
18
) ;
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
- } ) ;
21
19
22
- it ( 'should call emitAffectedFiles and iterate over result of emitted files' , async ( ) => {
23
- const parallelCompilation = {
20
+ parallelCompilation = {
24
21
emitAffectedFiles : mockEmitAffectedFiles ,
25
22
} as unknown as ParallelCompilation ;
26
- const javascriptTransformer = {
23
+ javascriptTransformer = {
27
24
transformData : mockTransformData ,
28
25
} 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
+ ] ) ;
30
34
31
35
await expect (
32
36
buildAndAnalyzeWithParallelCompilation (
@@ -36,16 +40,13 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
36
40
)
37
41
) . resolves . not . toThrow ( ) ;
38
42
expect ( mockEmitAffectedFiles ) . toHaveBeenCalledTimes ( 1 ) ;
43
+ expect ( mockTransformData ) . toHaveBeenCalledTimes ( 2 ) ;
39
44
} ) ;
40
45
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
+ ] ) ;
49
50
50
51
await expect (
51
52
buildAndAnalyzeWithParallelCompilation (
@@ -54,32 +55,20 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
54
55
javascriptTransformer
55
56
)
56
57
) . resolves . not . toThrow ( ) ;
57
- expect ( mockTransformData ) . toHaveBeenCalledTimes ( 2 ) ;
58
+
58
59
expect ( mockTransformData ) . toHaveBeenNthCalledWith (
59
60
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
+ '' ,
69
63
true ,
70
64
false
71
65
) ;
72
66
} ) ;
73
67
74
68
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
+ ] ) ;
83
72
await expect (
84
73
buildAndAnalyzeWithParallelCompilation (
85
74
parallelCompilation ,
@@ -88,20 +77,15 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
88
77
)
89
78
) . resolves . not . toThrow ( ) ;
90
79
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 ) ) ;
94
83
} ) ;
95
84
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
+ ] ) ;
105
89
await expect (
106
90
buildAndAnalyzeWithParallelCompilation (
107
91
parallelCompilation ,
@@ -110,18 +94,7 @@ describe('buildAndAnalyzeWithParallelCompilation', () => {
110
94
)
111
95
) . resolves . not . toThrow ( ) ;
112
96
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' ) ;
126
99
} ) ;
127
100
} ) ;
0 commit comments