3
3
* Testing all resolution paths: relative, absolute, prefix, and regular module requests
4
4
*/
5
5
6
+ import ModuleNotFoundError from 'webpack/lib/ModuleNotFoundError' ;
7
+ import LazySet from 'webpack/lib/util/LazySet' ;
8
+
6
9
import { resolveMatchedConfigs } from '../../../src/lib/sharing/resolveMatchedConfigs' ;
7
10
import type { ConsumeOptions } from '../../../src/declarations/plugins/sharing/ConsumeSharedModule' ;
8
11
9
12
jest . mock ( '@module-federation/sdk/normalize-webpack-path' , ( ) => ( {
10
13
normalizeWebpackPath : jest . fn ( ( path ) => path ) ,
11
14
} ) ) ;
12
15
13
- // Mock webpack classes
14
- jest . mock (
15
- 'webpack/lib/ModuleNotFoundError' ,
16
- ( ) =>
17
- jest . fn ( ) . mockImplementation ( ( module , err , details ) => {
18
- return { module, err, details } ;
19
- } ) ,
20
- {
21
- virtual : true ,
22
- } ,
23
- ) ;
24
- jest . mock (
25
- 'webpack/lib/util/LazySet' ,
26
- ( ) =>
27
- jest . fn ( ) . mockImplementation ( ( ) => ( {
28
- add : jest . fn ( ) ,
29
- addAll : jest . fn ( ) ,
30
- } ) ) ,
31
- { virtual : true } ,
32
- ) ;
33
-
34
16
describe ( 'resolveMatchedConfigs' , ( ) => {
35
17
let mockCompilation : any ;
36
18
let mockResolver : any ;
37
- let mockResolveContext : any ;
38
- let MockModuleNotFoundError : any ;
39
- let MockLazySet : any ;
40
19
41
20
beforeEach ( ( ) => {
42
21
jest . clearAllMocks ( ) ;
43
22
44
- // Get the mocked classes
45
- MockModuleNotFoundError = require ( 'webpack/lib/ModuleNotFoundError' ) ;
46
- MockLazySet = require ( 'webpack/lib/util/LazySet' ) ;
47
-
48
- mockResolveContext = {
49
- fileDependencies : { add : jest . fn ( ) , addAll : jest . fn ( ) } ,
50
- contextDependencies : { add : jest . fn ( ) , addAll : jest . fn ( ) } ,
51
- missingDependencies : { add : jest . fn ( ) , addAll : jest . fn ( ) } ,
52
- } ;
53
-
54
23
mockResolver = {
55
24
resolve : jest . fn ( ) ,
56
25
} ;
@@ -67,9 +36,6 @@ describe('resolveMatchedConfigs', () => {
67
36
fileDependencies : { addAll : jest . fn ( ) } ,
68
37
missingDependencies : { addAll : jest . fn ( ) } ,
69
38
} ;
70
-
71
- // Setup LazySet mock instances
72
- MockLazySet . mockImplementation ( ( ) => mockResolveContext . fileDependencies ) ;
73
39
} ) ;
74
40
75
41
describe ( 'relative path resolution' , ( ) => {
@@ -138,14 +104,15 @@ describe('resolveMatchedConfigs', () => {
138
104
expect ( result . unresolved . size ) . toBe ( 0 ) ;
139
105
expect ( result . prefixed . size ) . toBe ( 0 ) ;
140
106
expect ( mockCompilation . errors ) . toHaveLength ( 1 ) ;
141
- expect ( MockModuleNotFoundError ) . toHaveBeenCalledWith ( null , resolveError , {
107
+ const error = mockCompilation . errors [ 0 ] as InstanceType <
108
+ typeof ModuleNotFoundError
109
+ > ;
110
+ expect ( error ) . toBeInstanceOf ( ModuleNotFoundError ) ;
111
+ expect ( error . module ) . toBeNull ( ) ;
112
+ expect ( error . error ) . toBe ( resolveError ) ;
113
+ expect ( error . loc ) . toEqual ( {
142
114
name : 'shared module ./missing-module' ,
143
115
} ) ;
144
- expect ( mockCompilation . errors [ 0 ] ) . toEqual ( {
145
- module : null ,
146
- err : resolveError ,
147
- details : { name : 'shared module ./missing-module' } ,
148
- } ) ;
149
116
} ) ;
150
117
151
118
it ( 'should handle resolver returning false' , async ( ) => {
@@ -163,17 +130,15 @@ describe('resolveMatchedConfigs', () => {
163
130
164
131
expect ( result . resolved . size ) . toBe ( 0 ) ;
165
132
expect ( mockCompilation . errors ) . toHaveLength ( 1 ) ;
166
- expect ( MockModuleNotFoundError ) . toHaveBeenCalledWith (
167
- null ,
168
- expect . any ( Error ) ,
169
- { name : 'shared module ./invalid-module' } ,
170
- ) ;
171
- expect ( mockCompilation . errors [ 0 ] ) . toEqual ( {
172
- module : null ,
173
- err : expect . objectContaining ( {
174
- message : "Can't resolve ./invalid-module" ,
175
- } ) ,
176
- details : { name : 'shared module ./invalid-module' } ,
133
+ const error = mockCompilation . errors [ 0 ] as InstanceType <
134
+ typeof ModuleNotFoundError
135
+ > ;
136
+ expect ( error ) . toBeInstanceOf ( ModuleNotFoundError ) ;
137
+ expect ( error . module ) . toBeNull ( ) ;
138
+ expect ( error . error ) . toBeInstanceOf ( Error ) ;
139
+ expect ( error . error . message ) . toContain ( "Can't resolve ./invalid-module" ) ;
140
+ expect ( error . loc ) . toEqual ( {
141
+ name : 'shared module ./invalid-module' ,
177
142
} ) ;
178
143
} ) ;
179
144
@@ -459,12 +424,6 @@ describe('resolveMatchedConfigs', () => {
459
424
[ './relative' , { shareScope : 'default' } ] ,
460
425
] ;
461
426
462
- const resolveContext = {
463
- fileDependencies : { add : jest . fn ( ) , addAll : jest . fn ( ) } ,
464
- contextDependencies : { add : jest . fn ( ) , addAll : jest . fn ( ) } ,
465
- missingDependencies : { add : jest . fn ( ) , addAll : jest . fn ( ) } ,
466
- } ;
467
-
468
427
mockResolver . resolve . mockImplementation (
469
428
( context , basePath , request , rc , callback ) => {
470
429
// Simulate adding dependencies during resolution
@@ -475,22 +434,29 @@ describe('resolveMatchedConfigs', () => {
475
434
} ,
476
435
) ;
477
436
478
- // Update LazySet mock to return the actual resolve context
479
- MockLazySet . mockReturnValueOnce ( resolveContext . fileDependencies )
480
- . mockReturnValueOnce ( resolveContext . contextDependencies )
481
- . mockReturnValueOnce ( resolveContext . missingDependencies ) ;
482
-
483
437
await resolveMatchedConfigs ( mockCompilation , configs ) ;
484
438
485
- expect ( mockCompilation . contextDependencies . addAll ) . toHaveBeenCalledWith (
486
- resolveContext . contextDependencies ,
487
- ) ;
488
- expect ( mockCompilation . fileDependencies . addAll ) . toHaveBeenCalledWith (
489
- resolveContext . fileDependencies ,
439
+ expect ( mockCompilation . contextDependencies . addAll ) . toHaveBeenCalledTimes (
440
+ 1 ,
490
441
) ;
491
- expect ( mockCompilation . missingDependencies . addAll ) . toHaveBeenCalledWith (
492
- resolveContext . missingDependencies ,
442
+ expect ( mockCompilation . fileDependencies . addAll ) . toHaveBeenCalledTimes ( 1 ) ;
443
+ expect ( mockCompilation . missingDependencies . addAll ) . toHaveBeenCalledTimes (
444
+ 1 ,
493
445
) ;
446
+
447
+ const [ contextDeps ] =
448
+ mockCompilation . contextDependencies . addAll . mock . calls [ 0 ] ;
449
+ const [ fileDeps ] = mockCompilation . fileDependencies . addAll . mock . calls [ 0 ] ;
450
+ const [ missingDeps ] =
451
+ mockCompilation . missingDependencies . addAll . mock . calls [ 0 ] ;
452
+
453
+ expect ( contextDeps ) . toBeInstanceOf ( LazySet ) ;
454
+ expect ( fileDeps ) . toBeInstanceOf ( LazySet ) ;
455
+ expect ( missingDeps ) . toBeInstanceOf ( LazySet ) ;
456
+
457
+ expect ( contextDeps . has ( '/some/context' ) ) . toBe ( true ) ;
458
+ expect ( fileDeps . has ( '/some/file.js' ) ) . toBe ( true ) ;
459
+ expect ( missingDeps . has ( '/missing/file' ) ) . toBe ( true ) ;
494
460
} ) ;
495
461
} ) ;
496
462
0 commit comments