1
+ import type * as webpack from 'webpack'
2
+ import LicenseFileWriter from '../../src/LicenseFileWriter'
3
+ import WebpackChunkIterator from '../../src/WebpackChunkIterator'
1
4
import WebpackLicensePlugin from '../../src/WebpackLicensePlugin'
2
- import webpack = require( 'webpack' )
5
+
6
+ jest . mock ( '../../src/LicenseFileWriter' )
7
+ jest . mock ( '../../src/WebpackChunkIterator' )
3
8
4
9
const MockCompiler = jest . fn < webpack . Compiler , any [ ] > ( ( i ) => i )
5
10
const MockCompilation = jest . fn < webpack . compilation . Compilation , any [ ] > ( ( i ) => i )
6
11
const MockChunk = jest . fn < webpack . compilation . Chunk , any [ ] > ( ( i ) => i )
7
12
8
13
describe ( 'WebpackLicensePlugin' , ( ) => {
14
+ beforeEach ( ( ) => {
15
+ ( LicenseFileWriter as jest . Mock ) . mockReset ( ) ;
16
+ ( LicenseFileWriter as jest . Mock ) . mockImplementation ( ( ) => ( {
17
+ writeLicenseFiles : jest . fn ( )
18
+ } ) ) ;
19
+ ( WebpackChunkIterator as jest . Mock ) . mockReset ( ) ;
20
+ ( WebpackChunkIterator as jest . Mock ) . mockImplementation ( ( ) => ( {
21
+ iterateChunks : ( ) => [ ]
22
+ } ) )
23
+ } )
24
+
9
25
describe ( 'apply' , ( ) => {
10
- test ( 'taps into compilation hook if hooks are defined' , ( ) => {
26
+ test ( 'taps into compilation and watchRun hooks if hooks are defined' , ( ) => {
11
27
const compiler = new MockCompiler ( {
12
- hooks : { compilation : { tap : jest . fn ( ) } } ,
28
+ hooks : {
29
+ compilation : { tap : jest . fn ( ) } ,
30
+ watchRun : { tapAsync : jest . fn ( ) }
31
+ } ,
32
+
13
33
} )
14
34
const instance = new WebpackLicensePlugin ( { } )
15
35
instance . apply ( compiler )
@@ -19,15 +39,21 @@ describe('WebpackLicensePlugin', () => {
19
39
'webpack-license-plugin' ,
20
40
expect . any ( Function )
21
41
)
42
+ expect ( compiler . hooks . watchRun . tapAsync ) . toHaveBeenCalledTimes ( 1 )
43
+ expect ( compiler . hooks . watchRun . tapAsync ) . toHaveBeenCalledWith (
44
+ 'webpack-license-plugin' ,
45
+ expect . any ( Function )
46
+ )
22
47
} )
23
48
24
49
test ( 'plugs into compilation otherwise' , ( ) => {
25
50
const compiler = new MockCompiler ( { plugin : jest . fn ( ) } )
26
51
const instance = new WebpackLicensePlugin ( )
27
52
instance . apply ( compiler )
28
53
29
- expect ( compiler . plugin ) . toHaveBeenCalledTimes ( 1 )
54
+ expect ( compiler . plugin ) . toHaveBeenCalledTimes ( 2 )
30
55
expect ( compiler . plugin ) . toHaveBeenCalledWith ( 'compilation' , expect . any ( Function ) )
56
+ expect ( compiler . plugin ) . toHaveBeenCalledWith ( 'watchRun' , expect . any ( Function ) )
31
57
} )
32
58
} )
33
59
@@ -57,18 +83,143 @@ describe('WebpackLicensePlugin', () => {
57
83
} )
58
84
59
85
describe ( 'handleChunkAssetOptimization' , ( ) => {
86
+ const createMockCompilation = ( name : string , isChild : boolean ) => new MockCompilation ( {
87
+ assets : [ ] ,
88
+ errors : [ ] ,
89
+ warnings : [ ] ,
90
+ compiler : {
91
+ name,
92
+ isChild : ( ) => isChild
93
+ }
94
+ } )
95
+
60
96
test ( 'calls plugin mechanism callback when done' , async ( ) => {
61
97
const instance = new WebpackLicensePlugin ( )
62
98
const callback = jest . fn ( )
63
99
64
100
await instance . handleChunkAssetOptimization (
65
101
new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
66
- new MockCompilation ( { assets : [ ] , errors : [ ] , warnings : [ ] } ) ,
102
+ createMockCompilation ( 'mockCompiler' , false ) ,
67
103
[ new MockChunk ( ) ] ,
68
104
callback
69
105
)
70
106
71
107
expect ( callback ) . toHaveBeenCalledTimes ( 1 )
72
108
} )
109
+
110
+ test ( 'calls writeLicenseFiles with all filenames' , async ( ) => {
111
+ ( WebpackChunkIterator as jest . Mock ) . mockReset ( ) ;
112
+ ( WebpackChunkIterator as jest . Mock )
113
+ . mockImplementationOnce ( ( ) => ( {
114
+ iterateChunks : ( ) => [ 'filename1' , 'filename2' ]
115
+ } ) )
116
+ . mockImplementationOnce ( ( ) => ( {
117
+ iterateChunks : ( ) => [ 'filename1' , 'filename3' , 'filename4' ]
118
+ } ) )
119
+
120
+ const writeLicenseFiles = jest . fn ( ) ;
121
+ ( LicenseFileWriter as jest . Mock ) . mockReset ( ) ;
122
+ ( LicenseFileWriter as jest . Mock ) . mockImplementationOnce ( ( ) => ( { writeLicenseFiles } ) )
123
+
124
+ const instance = new WebpackLicensePlugin ( )
125
+
126
+ const callback1 = jest . fn ( )
127
+ const mockCompilation1 = createMockCompilation ( 'mockCompiler1' , true )
128
+ await instance . handleChunkAssetOptimization (
129
+ new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
130
+ mockCompilation1 ,
131
+ [ new MockChunk ( ) ] ,
132
+ callback1
133
+ )
134
+
135
+ const callback2 = jest . fn ( )
136
+ const mockCompilation2 = createMockCompilation ( 'mockCompiler2' , false )
137
+ await instance . handleChunkAssetOptimization (
138
+ new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
139
+ mockCompilation2 ,
140
+ [ new MockChunk ( ) ] ,
141
+ callback2
142
+ )
143
+
144
+ expect ( callback1 ) . toHaveBeenCalledTimes ( 1 )
145
+ expect ( callback2 ) . toHaveBeenCalledTimes ( 1 )
146
+
147
+ expect ( mockCompilation1 . errors ) . toEqual ( [ ] )
148
+ expect ( mockCompilation2 . errors ) . toEqual ( [ ] )
149
+
150
+ expect ( writeLicenseFiles ) . toHaveBeenCalledWith ( [ 'filename1' , 'filename2' , 'filename3' , 'filename4' ] , expect . anything ( ) )
151
+ expect ( writeLicenseFiles ) . toHaveBeenCalledTimes ( 1 )
152
+ } )
153
+
154
+ test ( 'pushes error if handleChunkAssetOptimization is called again after files were written' , async ( ) => {
155
+ const instance = new WebpackLicensePlugin ( )
156
+
157
+ const callback1 = jest . fn ( )
158
+ const mockCompilation1 = createMockCompilation ( 'mockCompiler1' , false )
159
+ await instance . handleChunkAssetOptimization (
160
+ new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
161
+ mockCompilation1 ,
162
+ [ new MockChunk ( ) ] ,
163
+ callback1
164
+ )
165
+
166
+ const callback2 = jest . fn ( )
167
+ const mockCompilation2 = createMockCompilation ( 'mockCompiler2' , true )
168
+ await instance . handleChunkAssetOptimization (
169
+ new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
170
+ mockCompilation2 ,
171
+ [ new MockChunk ( ) ] ,
172
+ callback2
173
+ )
174
+
175
+ expect ( callback1 ) . toHaveBeenCalledTimes ( 1 )
176
+ expect ( callback2 ) . toHaveBeenCalledTimes ( 1 )
177
+
178
+ expect ( mockCompilation1 . errors ) . toEqual ( [ ] )
179
+ expect ( mockCompilation2 . errors ) . toEqual ( [ "WebpackLicensePlugin: Found licenses after license files were already created.\nIf you see this message, you ran into an edge case we thought would not happen. Please open an isssue at https://github.com/codepunkt/webpack-license-plugin/issues with details of your webpack configuration so we can invastigate it further.\ncompiler: mockCompiler1, isChild: false\ncompiler: mockCompiler2, isChild: true" ] )
180
+ } )
181
+
182
+ test ( 'reset when handleWatchRun is called' , async ( ) => {
183
+ const instance = new WebpackLicensePlugin ( )
184
+
185
+ const callback1 = jest . fn ( )
186
+ const mockCompilation1 = createMockCompilation ( 'mockCompiler1' , false )
187
+ await instance . handleChunkAssetOptimization (
188
+ new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
189
+ mockCompilation1 ,
190
+ [ new MockChunk ( ) ] ,
191
+ callback1
192
+ )
193
+
194
+ const callbackWatchRun = jest . fn ( )
195
+ await instance . handleWatchRun ( undefined , callbackWatchRun )
196
+
197
+ const callback2 = jest . fn ( )
198
+ const mockCompilation2 = createMockCompilation ( 'mockCompiler2' , false )
199
+ await instance . handleChunkAssetOptimization (
200
+ new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
201
+ mockCompilation2 ,
202
+ [ new MockChunk ( ) ] ,
203
+ callback2
204
+ )
205
+
206
+ const callback3 = jest . fn ( )
207
+ const mockCompilation3 = createMockCompilation ( 'mockCompiler3' , true )
208
+ await instance . handleChunkAssetOptimization (
209
+ new MockCompiler ( { inputFileSystem : 'a' , options : { context : 'b' } } ) ,
210
+ mockCompilation3 ,
211
+ [ new MockChunk ( ) ] ,
212
+ callback3
213
+ )
214
+
215
+ expect ( callback1 ) . toHaveBeenCalledTimes ( 1 )
216
+ expect ( callbackWatchRun ) . toHaveBeenCalledTimes ( 1 )
217
+ expect ( callback2 ) . toHaveBeenCalledTimes ( 1 )
218
+ expect ( callback3 ) . toHaveBeenCalledTimes ( 1 )
219
+
220
+ expect ( mockCompilation1 . errors ) . toEqual ( [ ] )
221
+ expect ( mockCompilation2 . errors ) . toEqual ( [ ] )
222
+ expect ( mockCompilation3 . errors ) . toEqual ( [ "WebpackLicensePlugin: Found licenses after license files were already created.\nIf you see this message, you ran into an edge case we thought would not happen. Please open an isssue at https://github.com/codepunkt/webpack-license-plugin/issues with details of your webpack configuration so we can invastigate it further.\ncompiler: mockCompiler2, isChild: false\ncompiler: mockCompiler3, isChild: true" ] )
223
+ } )
73
224
} )
74
225
} )
0 commit comments