@@ -185,7 +185,7 @@ describe("AstAnalyser", (t) => {
185
185
analyser . analyse ( "const foo = 'bar';" , {
186
186
initialize : "foo"
187
187
} ) ;
188
- } ) ;
188
+ } , / o p t i o n s . i n i t i a l i z e m u s t b e a f u n c t i o n / ) ;
189
189
} ) ;
190
190
191
191
it ( "should call the initialize function" , ( t ) => {
@@ -216,7 +216,7 @@ describe("AstAnalyser", (t) => {
216
216
analyser . analyse ( "const foo = 'bar';" , {
217
217
finalize : "foo"
218
218
} ) ;
219
- } ) ;
219
+ } , / o p t i o n s . f i n a l i z e m u s t b e a f u n c t i o n / ) ;
220
220
} ) ;
221
221
222
222
it ( "should call the finalize function" , ( t ) => {
@@ -254,30 +254,115 @@ describe("AstAnalyser", (t) => {
254
254
} ) ;
255
255
} ) ;
256
256
257
- it ( "remove the packageName from the dependencies list" , async ( ) => {
258
- const result = await getAnalyser ( ) . analyseFile (
259
- new URL ( "depName.js" , FIXTURE_URL ) ,
260
- { module : false , packageName : "foobar" }
261
- ) ;
262
-
263
- assert . ok ( result . ok ) ;
264
- assert . strictEqual ( result . warnings . length , 0 ) ;
265
- assert . deepEqual ( [ ...result . dependencies . keys ( ) ] ,
266
- [ "open" ]
267
- ) ;
268
- } ) ;
257
+ describe ( "analyseFile" , ( ) => {
258
+ it ( "remove the packageName from the dependencies list" , async ( ) => {
259
+ const result = await getAnalyser ( ) . analyseFile (
260
+ new URL ( "depName.js" , FIXTURE_URL ) ,
261
+ { module : false , packageName : "foobar" }
262
+ ) ;
263
+
264
+ assert . ok ( result . ok ) ;
265
+ assert . strictEqual ( result . warnings . length , 0 ) ;
266
+ assert . deepEqual ( [ ...result . dependencies . keys ( ) ] ,
267
+ [ "open" ]
268
+ ) ;
269
+ } ) ;
270
+
271
+ it ( "should fail with a parsing error" , async ( ) => {
272
+ const result = await getAnalyser ( ) . analyseFile (
273
+ new URL ( "parsingError.js" , FIXTURE_URL ) ,
274
+ { module : false , packageName : "foobar" }
275
+ ) ;
276
+
277
+ assert . strictEqual ( result . ok , false ) ;
278
+ assert . strictEqual ( result . warnings . length , 1 ) ;
279
+
280
+ const parsingError = result . warnings [ 0 ] ;
281
+ assert . strictEqual ( parsingError . kind , "parsing-error" ) ;
282
+ } ) ;
283
+
284
+ describe ( "hooks" , ( ) => {
285
+ const analyser = new AstAnalyser ( ) ;
286
+ const url = new URL ( "depName.js" , FIXTURE_URL ) ;
287
+
288
+ describe ( "initialize" , ( ) => {
289
+ it ( "should throw if initialize is not a function" , async ( ) => {
290
+ const res = await analyser . analyseFile (
291
+ url , {
292
+ initialize : "foo"
293
+ } ) ;
269
294
270
- it ( "should fail with a parsing error" , async ( ) => {
271
- const result = await getAnalyser ( ) . analyseFile (
272
- new URL ( "parsingError.js" , FIXTURE_URL ) ,
273
- { module : false , packageName : "foobar" }
274
- ) ;
295
+ assert . strictEqual ( res . ok , false ) ;
296
+ assert . strictEqual ( res . warnings [ 0 ] . value , "options.initialize must be a function" ) ;
297
+ assert . strictEqual ( res . warnings [ 0 ] . kind , "parsing-error" ) ;
298
+ } ) ;
275
299
276
- assert . strictEqual ( result . ok , false ) ;
277
- assert . strictEqual ( result . warnings . length , 1 ) ;
300
+ it ( "should call the initialize function" , async ( t ) => {
301
+ const initialize = t . mock . fn ( ) ;
302
+
303
+ await analyser . analyseFile ( url , {
304
+ initialize
305
+ } ) ;
306
+
307
+ assert . strictEqual ( initialize . mock . callCount ( ) , 1 ) ;
308
+ } ) ;
278
309
279
- const parsingError = result . warnings [ 0 ] ;
280
- assert . strictEqual ( parsingError . kind , "parsing-error" ) ;
310
+ it ( "should pass the source file as first argument" , async ( t ) => {
311
+ const initialize = t . mock . fn ( ) ;
312
+
313
+ await analyser . analyseFile ( url , {
314
+ initialize
315
+ } ) ;
316
+
317
+ assert . strictEqual ( initialize . mock . calls [ 0 ] . arguments [ 0 ] instanceof SourceFile , true ) ;
318
+ } ) ;
319
+ } ) ;
320
+
321
+ describe ( "finalize" , ( ) => {
322
+ it ( "should throw if finalize is not a function" , async ( ) => {
323
+ const res = await analyser . analyseFile (
324
+ url , {
325
+ finalize : "foo"
326
+ } ) ;
327
+
328
+ assert . strictEqual ( res . ok , false ) ;
329
+ assert . strictEqual ( res . warnings [ 0 ] . value , "options.finalize must be a function" ) ;
330
+ assert . strictEqual ( res . warnings [ 0 ] . kind , "parsing-error" ) ;
331
+ } ) ;
332
+
333
+ it ( "should call the finalize function" , async ( t ) => {
334
+ const finalize = t . mock . fn ( ) ;
335
+
336
+ await analyser . analyseFile ( url , {
337
+ finalize
338
+ } ) ;
339
+
340
+ assert . strictEqual ( finalize . mock . callCount ( ) , 1 ) ;
341
+ } ) ;
342
+
343
+ it ( "should pass the source file as first argument" , async ( t ) => {
344
+ const finalize = t . mock . fn ( ) ;
345
+
346
+ await analyser . analyseFile ( url , {
347
+ finalize
348
+ } ) ;
349
+
350
+ assert . strictEqual ( finalize . mock . calls [ 0 ] . arguments [ 0 ] instanceof SourceFile , true ) ;
351
+ } ) ;
352
+ } ) ;
353
+
354
+
355
+ it ( "intialize should be called before finalize" , async ( ) => {
356
+ const calls = [ ] ;
357
+
358
+ await analyser . analyseFile ( url , {
359
+ initialize : ( ) => calls . push ( "initialize" ) ,
360
+ finalize : ( ) => calls . push ( "finalize" )
361
+ } ) ;
362
+
363
+ assert . deepEqual ( calls , [ "initialize" , "finalize" ] ) ;
364
+ } ) ;
365
+ } ) ;
281
366
} ) ;
282
367
283
368
describe ( "prepareSource" , ( ) => {
0 commit comments