@@ -365,6 +365,116 @@ describe("AstAnalyser", (t) => {
365
365
} ) ;
366
366
} ) ;
367
367
368
+ describe ( "analyseFileSync" , ( ) => {
369
+ it ( "remove the packageName from the dependencies list" , ( ) => {
370
+ const result = getAnalyser ( ) . analyseFileSync (
371
+ new URL ( "depName.js" , FIXTURE_URL ) ,
372
+ { module : false , packageName : "foobar" }
373
+ ) ;
374
+
375
+ assert . ok ( result . ok ) ;
376
+ assert . strictEqual ( result . warnings . length , 0 ) ;
377
+ assert . deepEqual ( [ ...result . dependencies . keys ( ) ] ,
378
+ [ "open" ]
379
+ ) ;
380
+ } ) ;
381
+
382
+ it ( "should fail with a parsing error" , ( ) => {
383
+ const result = getAnalyser ( ) . analyseFileSync (
384
+ new URL ( "parsingError.js" , FIXTURE_URL ) ,
385
+ { module : false , packageName : "foobar" }
386
+ ) ;
387
+
388
+ assert . strictEqual ( result . ok , false ) ;
389
+ assert . strictEqual ( result . warnings . length , 1 ) ;
390
+
391
+ const parsingError = result . warnings [ 0 ] ;
392
+ assert . strictEqual ( parsingError . kind , "parsing-error" ) ;
393
+ } ) ;
394
+
395
+ describe ( "hooks" , ( ) => {
396
+ const url = new URL ( "depName.js" , FIXTURE_URL ) ;
397
+
398
+ describe ( "initialize" , ( ) => {
399
+ it ( "should throw if initialize is not a function" , ( ) => {
400
+ const res = getAnalyser ( ) . analyseFileSync (
401
+ url , {
402
+ initialize : "foo"
403
+ } ) ;
404
+
405
+ assert . strictEqual ( res . ok , false ) ;
406
+ assert . strictEqual ( res . warnings [ 0 ] . value , "options.initialize must be a function" ) ;
407
+ assert . strictEqual ( res . warnings [ 0 ] . kind , "parsing-error" ) ;
408
+ } ) ;
409
+
410
+ it ( "should call the initialize function" , ( t ) => {
411
+ const initialize = t . mock . fn ( ) ;
412
+
413
+ getAnalyser ( ) . analyseFileSync ( url , {
414
+ initialize
415
+ } ) ;
416
+
417
+ assert . strictEqual ( initialize . mock . callCount ( ) , 1 ) ;
418
+ } ) ;
419
+
420
+ it ( "should pass the source file as first argument" , ( t ) => {
421
+ const initialize = t . mock . fn ( ) ;
422
+
423
+ getAnalyser ( ) . analyseFileSync ( url , {
424
+ initialize
425
+ } ) ;
426
+
427
+ assert . strictEqual ( initialize . mock . calls [ 0 ] . arguments [ 0 ] instanceof SourceFile , true ) ;
428
+ } ) ;
429
+ } ) ;
430
+
431
+ describe ( "finalize" , ( ) => {
432
+ it ( "should throw if finalize is not a function" , ( ) => {
433
+ const res = getAnalyser ( ) . analyseFileSync (
434
+ url , {
435
+ finalize : "foo"
436
+ } ) ;
437
+
438
+ assert . strictEqual ( res . ok , false ) ;
439
+ assert . strictEqual ( res . warnings [ 0 ] . value , "options.finalize must be a function" ) ;
440
+ assert . strictEqual ( res . warnings [ 0 ] . kind , "parsing-error" ) ;
441
+ } ) ;
442
+
443
+ it ( "should call the finalize function" , ( t ) => {
444
+ const finalize = t . mock . fn ( ) ;
445
+
446
+ getAnalyser ( ) . analyseFileSync ( url , {
447
+ finalize
448
+ } ) ;
449
+
450
+ assert . strictEqual ( finalize . mock . callCount ( ) , 1 ) ;
451
+ } ) ;
452
+
453
+ it ( "should pass the source file as first argument" , ( t ) => {
454
+ const finalize = t . mock . fn ( ) ;
455
+
456
+ getAnalyser ( ) . analyseFileSync ( url , {
457
+ finalize
458
+ } ) ;
459
+
460
+ assert . strictEqual ( finalize . mock . calls [ 0 ] . arguments [ 0 ] instanceof SourceFile , true ) ;
461
+ } ) ;
462
+ } ) ;
463
+
464
+
465
+ it ( "intialize should be called before finalize" , ( ) => {
466
+ const calls = [ ] ;
467
+
468
+ getAnalyser ( ) . analyseFileSync ( url , {
469
+ initialize : ( ) => calls . push ( "initialize" ) ,
470
+ finalize : ( ) => calls . push ( "finalize" )
471
+ } ) ;
472
+
473
+ assert . deepEqual ( calls , [ "initialize" , "finalize" ] ) ;
474
+ } ) ;
475
+ } ) ;
476
+ } ) ;
477
+
368
478
describe ( "prepareSource" , ( ) => {
369
479
it ( "should remove shebang at the start of the file" , ( t ) => {
370
480
const source = "#!/usr/bin/env node\nconst hello = \"world\";" ;
0 commit comments