@@ -20,8 +20,14 @@ import { WatchNotifier } from '../src/watcher'
20
20
21
21
const puppeteerTimeoutMs = 60000
22
22
23
+ let mkdirSpy : jest . SpiedFunction < typeof fs . promises . mkdir >
24
+
23
25
jest . mock ( 'fs' )
24
26
27
+ beforeEach ( ( ) => {
28
+ mkdirSpy = jest . spyOn ( fs . promises , 'mkdir' ) . mockImplementation ( )
29
+ } )
30
+
25
31
afterAll ( ( ) => Converter . closeBrowser ( ) )
26
32
afterEach ( ( ) => jest . restoreAllMocks ( ) )
27
33
@@ -58,7 +64,7 @@ describe('Converter', () => {
58
64
globalDirectives : { theme : 'default' } ,
59
65
imageScale : 2 ,
60
66
lang : 'fr' ,
61
- options : < Options > { html : true } ,
67
+ options : { html : true } as Options ,
62
68
server : false ,
63
69
template : 'test-template' ,
64
70
templateOption : { } ,
@@ -485,12 +491,12 @@ transition:
485
491
describe ( '#convertFile' , ( ) => {
486
492
it ( 'rejects Promise when specified file is not found' , ( ) =>
487
493
expect (
488
- ( instance ( ) as any ) . convertFile ( new File ( '_NOT_FOUND_MARKDOWN_' ) )
494
+ instance ( ) . convertFile ( new File ( '_NOT_FOUND_MARKDOWN_' ) )
489
495
) . rejects . toBeTruthy ( ) )
490
496
491
497
it ( 'converts markdown file and save as html file by default' , async ( ) => {
492
- const write = ( < any > fs ) . __mockWriteFile ( )
493
- await ( < any > instance ( ) ) . convertFile ( new File ( onePath ) )
498
+ const write = ( fs as any ) . __mockWriteFile ( )
499
+ await instance ( ) . convertFile ( new File ( onePath ) )
494
500
495
501
expect ( write ) . toHaveBeenCalledWith (
496
502
`${ onePath . slice ( 0 , - 3 ) } .html` ,
@@ -500,9 +506,9 @@ transition:
500
506
} )
501
507
502
508
it ( 'converts markdown file and save to specified path when output is defined' , async ( ) => {
503
- const write = ( < any > fs ) . __mockWriteFile ( )
509
+ const write = ( fs as any ) . __mockWriteFile ( )
504
510
const output = './specified.html'
505
- await ( < any > instance ( { output } ) ) . convertFile ( new File ( twoPath ) )
511
+ await instance ( { output } ) . convertFile ( new File ( twoPath ) )
506
512
507
513
expect ( write ) . toHaveBeenCalledWith (
508
514
output ,
@@ -511,19 +517,40 @@ transition:
511
517
)
512
518
} )
513
519
520
+ it ( 'tries to create the directory of output file when saving' , async ( ) => {
521
+ const write = ( fs as any ) . __mockWriteFile ( )
522
+ const output = path . resolve ( __dirname , '__test_dir__/out.html' )
523
+
524
+ await instance ( { output } ) . convertFile ( new File ( twoPath ) )
525
+
526
+ expect ( write ) . toHaveBeenCalled ( )
527
+ expect ( mkdirSpy ) . toHaveBeenCalledWith (
528
+ path . resolve ( __dirname , '__test_dir__' ) ,
529
+ { recursive : true }
530
+ )
531
+ } )
532
+
533
+ it ( 'does not try to create the directory of output file when saving to the root' , async ( ) => {
534
+ const write = ( fs as any ) . __mockWriteFile ( )
535
+ const output = '/out.html'
536
+
537
+ await instance ( { output } ) . convertFile ( new File ( twoPath ) )
538
+
539
+ expect ( write ) . toHaveBeenCalled ( )
540
+ expect ( mkdirSpy ) . not . toHaveBeenCalled ( )
541
+ } )
542
+
514
543
it ( 'converts markdown file but not save when output is stdout' , async ( ) => {
515
- const write = ( < any > fs ) . __mockWriteFile ( )
544
+ const write = ( fs as any ) . __mockWriteFile ( )
516
545
const stdout = jest . spyOn ( process . stdout , 'write' ) . mockImplementation ( )
517
546
518
547
const output = '-'
519
- const ret = await ( < any > instance ( { output } ) ) . convertFile (
520
- new File ( threePath )
521
- )
548
+ const ret = await instance ( { output } ) . convertFile ( new File ( threePath ) )
522
549
523
550
expect ( write ) . not . toHaveBeenCalled ( )
524
551
expect ( stdout ) . toHaveBeenCalledTimes ( 1 )
525
552
expect ( ret . file . path ) . toBe ( threePath )
526
- expect ( ret . newFile . type ) . toBe ( FileType . StandardIO )
553
+ expect ( ret . newFile ? .type ) . toBe ( FileType . StandardIO )
527
554
} )
528
555
529
556
describe ( 'when convert type is PDF' , ( ) => {
@@ -533,7 +560,7 @@ transition:
533
560
it (
534
561
'converts markdown file into PDF' ,
535
562
async ( ) => {
536
- const write = ( < any > fs ) . __mockWriteFile ( )
563
+ const write = ( fs as any ) . __mockWriteFile ( )
537
564
const opts = { output : 'test.pdf' }
538
565
const ret = await pdfInstance ( opts ) . convertFile ( new File ( onePath ) )
539
566
const pdf : Buffer = write . mock . calls [ 0 ] [ 1 ]
@@ -551,7 +578,7 @@ transition:
551
578
it (
552
579
'assigns meta info thorugh pdf-lib' ,
553
580
async ( ) => {
554
- const write = ( < any > fs ) . __mockWriteFile ( )
581
+ const write = ( fs as any ) . __mockWriteFile ( )
555
582
556
583
await pdfInstance ( {
557
584
output : 'test.pdf' ,
@@ -580,7 +607,7 @@ transition:
580
607
async ( ) => {
581
608
const file = new File ( onePath )
582
609
583
- const fileCleanup = jest . spyOn ( < any > File . prototype , 'cleanup' )
610
+ const fileCleanup = jest . spyOn ( File . prototype as any , 'cleanup' )
584
611
const fileSave = jest
585
612
. spyOn ( File . prototype , 'save' )
586
613
. mockImplementation ( )
@@ -614,7 +641,7 @@ transition:
614
641
it (
615
642
'assigns presenter notes as annotation of PDF' ,
616
643
async ( ) => {
617
- const write = ( < any > fs ) . __mockWriteFile ( )
644
+ const write = ( fs as any ) . __mockWriteFile ( )
618
645
619
646
await pdfInstance ( {
620
647
output : 'test.pdf' ,
@@ -637,7 +664,7 @@ transition:
637
664
)
638
665
639
666
it ( 'sets a comment author to notes if set author global directive' , async ( ) => {
640
- const write = ( < any > fs ) . __mockWriteFile ( )
667
+ const write = ( fs as any ) . __mockWriteFile ( )
641
668
642
669
await pdfInstance ( {
643
670
output : 'test.pdf' ,
@@ -673,7 +700,7 @@ transition:
673
700
let write : jest . Mock
674
701
675
702
beforeEach ( ( ) => {
676
- write = ( < any > fs ) . __mockWriteFile ( )
703
+ write = ( fs as any ) . __mockWriteFile ( )
677
704
} )
678
705
679
706
const converter = ( opts : Partial < ConverterOption > = { } ) =>
@@ -781,7 +808,7 @@ transition:
781
808
let write : jest . Mock
782
809
783
810
beforeEach ( ( ) => {
784
- write = ( < any > fs ) . __mockWriteFile ( )
811
+ write = ( fs as any ) . __mockWriteFile ( )
785
812
} )
786
813
787
814
it (
@@ -851,7 +878,7 @@ transition:
851
878
let write : jest . Mock
852
879
853
880
beforeEach ( ( ) => {
854
- write = ( < any > fs ) . __mockWriteFile ( )
881
+ write = ( fs as any ) . __mockWriteFile ( )
855
882
} )
856
883
857
884
it (
@@ -929,7 +956,7 @@ transition:
929
956
pages : true ,
930
957
type : ConvertType . png ,
931
958
} )
932
- write = ( < any > fs ) . __mockWriteFile ( )
959
+ write = ( fs as any ) . __mockWriteFile ( )
933
960
} )
934
961
935
962
it (
@@ -950,9 +977,9 @@ transition:
950
977
const notesInstance = ( opts : Partial < ConverterOption > = { } ) =>
951
978
instance ( { ...opts , type : ConvertType . notes } )
952
979
953
- const write = ( < any > fs ) . __mockWriteFile ( )
980
+ const write = ( fs as any ) . __mockWriteFile ( )
954
981
const output = './specified.txt'
955
- const ret = await ( < any > notesInstance ( { output } ) ) . convertFile (
982
+ const ret = await notesInstance ( { output } ) . convertFile (
956
983
new File ( threePath )
957
984
)
958
985
const notes : Buffer = write . mock . calls [ 0 ] [ 1 ]
@@ -969,9 +996,9 @@ transition:
969
996
const notesInstance = ( opts : Partial < ConverterOption > = { } ) =>
970
997
instance ( { ...opts , type : ConvertType . notes } )
971
998
972
- const write = ( < any > fs ) . __mockWriteFile ( )
999
+ const write = ( fs as any ) . __mockWriteFile ( )
973
1000
const output = './specified.txt'
974
- const ret = await ( < any > notesInstance ( { output } ) ) . convertFile (
1001
+ const ret = await notesInstance ( { output } ) . convertFile (
975
1002
new File ( onePath )
976
1003
)
977
1004
const notes : Buffer = write . mock . calls [ 0 ] [ 1 ]
@@ -991,7 +1018,7 @@ transition:
991
1018
describe ( '#convertFiles' , ( ) => {
992
1019
describe ( 'with multiple files' , ( ) => {
993
1020
it ( 'converts passed files' , async ( ) => {
994
- const write = ( < any > fs ) . __mockWriteFile ( )
1021
+ const write = ( fs as any ) . __mockWriteFile ( )
995
1022
996
1023
await instance ( ) . convertFiles ( [ new File ( onePath ) , new File ( twoPath ) ] )
997
1024
expect ( write ) . toHaveBeenCalledTimes ( 2 )
@@ -1008,7 +1035,7 @@ transition:
1008
1035
) . rejects . toBeInstanceOf ( CLIError ) )
1009
1036
1010
1037
it ( 'converts passed files when output is stdout' , async ( ) => {
1011
- const write = ( < any > fs ) . __mockWriteFile ( )
1038
+ const write = ( fs as any ) . __mockWriteFile ( )
1012
1039
const stdout = jest . spyOn ( process . stdout , 'write' ) . mockImplementation ( )
1013
1040
const files = [ new File ( onePath ) , new File ( twoPath ) ]
1014
1041
0 commit comments