@@ -151,6 +151,8 @@ let mockFs: Record<Config.Path, string>;
151
151
let object : < T > ( input : T ) => T ;
152
152
let writeFileAtomic : typeof import ( 'write-file-atomic' ) ;
153
153
154
+ const filename = '/fruits/banana.js' ;
155
+
154
156
jest . mock ( 'write-file-atomic' , ( ) => ( {
155
157
sync : jest . fn ( ) . mockImplementation ( ( filePath , data ) => {
156
158
mockFs [ filePath ] = data ;
@@ -228,20 +230,18 @@ describe('ScriptTransformer', () => {
228
230
it ( 'transforms a file properly' , ( ) => {
229
231
const scriptTransformer = new ScriptTransformer ( config ) ;
230
232
const transformedBananaWithCoverage = scriptTransformer . transform (
231
- '/fruits/banana.js' ,
233
+ filename ,
232
234
getCoverageOptions ( { collectCoverage : true } ) ,
235
+ mockFs [ filename ] ,
233
236
) ;
234
237
235
238
expect ( wrap ( transformedBananaWithCoverage . code ) ) . toMatchSnapshot ( ) ;
236
239
237
- // no-cache case
238
- expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
239
- expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana.js' , 'utf8' ) ;
240
-
241
240
// in-memory cache
242
241
const transformedBananaWithCoverageAgain = scriptTransformer . transform (
243
- '/fruits/banana.js' ,
242
+ filename ,
244
243
getCoverageOptions ( { collectCoverage : true } ) ,
244
+ mockFs [ filename ] ,
245
245
) ;
246
246
expect ( transformedBananaWithCoverageAgain ) . toBe (
247
247
transformedBananaWithCoverage ,
@@ -250,6 +250,7 @@ describe('ScriptTransformer', () => {
250
250
const transformedKiwiWithCoverage = scriptTransformer . transform (
251
251
'/fruits/kiwi.js' ,
252
252
getCoverageOptions ( { collectCoverage : true } ) ,
253
+ mockFs [ '/fruits/kiwi.js' ] ,
253
254
) ;
254
255
expect ( wrap ( transformedKiwiWithCoverage . code ) ) . toMatchSnapshot ( ) ;
255
256
@@ -262,6 +263,7 @@ describe('ScriptTransformer', () => {
262
263
const transformedKiwiWithoutCoverage = scriptTransformer . transform (
263
264
'/fruits/kiwi.js' ,
264
265
getCoverageOptions ( { collectCoverage : false } ) ,
266
+ mockFs [ '/fruits/kiwi.js' ] ,
265
267
) ;
266
268
267
269
expect ( transformedKiwiWithoutCoverage . code ) . not . toEqual (
@@ -299,7 +301,7 @@ describe('ScriptTransformer', () => {
299
301
const scriptTransformer = new ScriptTransformer ( config ) ;
300
302
301
303
const incorrectReturnValues = [
302
- [ undefined , '/fruits/banana.js' ] ,
304
+ [ undefined , filename ] ,
303
305
[ { a : 'a' } , '/fruits/kiwi.js' ] ,
304
306
[ [ ] , '/fruits/grapefruit.js' ] ,
305
307
] ;
@@ -310,12 +312,16 @@ describe('ScriptTransformer', () => {
310
312
returnValue ,
311
313
) ;
312
314
expect ( ( ) =>
313
- scriptTransformer . transform ( filePath , getCoverageOptions ( ) ) ,
315
+ scriptTransformer . transform (
316
+ filePath ,
317
+ getCoverageOptions ( ) ,
318
+ mockFs [ filename ] ,
319
+ ) ,
314
320
) . toThrow ( 'must return a string' ) ;
315
321
} ) ;
316
322
317
323
const correctReturnValues = [
318
- [ 'code' , '/fruits/banana.js' ] ,
324
+ [ 'code' , filename ] ,
319
325
[ { code : 'code' } , '/fruits/kiwi.js' ] ,
320
326
] ;
321
327
@@ -325,7 +331,11 @@ describe('ScriptTransformer', () => {
325
331
returnValue ,
326
332
) ;
327
333
expect ( ( ) =>
328
- scriptTransformer . transform ( filePath , getCoverageOptions ( ) ) ,
334
+ scriptTransformer . transform (
335
+ filePath ,
336
+ getCoverageOptions ( ) ,
337
+ mockFs [ filename ] ,
338
+ ) ,
329
339
) . not . toThrow ( ) ;
330
340
} ) ;
331
341
} ,
@@ -338,7 +348,7 @@ describe('ScriptTransformer', () => {
338
348
} ;
339
349
const scriptTransformer = new ScriptTransformer ( config ) ;
340
350
expect ( ( ) =>
341
- scriptTransformer . transformSource ( 'sample.js' , '' , false ) ,
351
+ scriptTransformer . transformSource ( 'sample.js' , '' , { instrument : false } ) ,
342
352
) . toThrow ( 'Jest: a transform must export a `process` function.' ) ;
343
353
} ) ;
344
354
@@ -355,7 +365,7 @@ describe('ScriptTransformer', () => {
355
365
} ;
356
366
const scriptTransformer = new ScriptTransformer ( config ) ;
357
367
expect ( ( ) =>
358
- scriptTransformer . transformSource ( 'sample.js' , '' , false ) ,
368
+ scriptTransformer . transformSource ( 'sample.js' , '' , { instrument : false } ) ,
359
369
) . toThrow ( 'Jest: a transform must export a `process` function.' ) ;
360
370
} ) ;
361
371
@@ -366,16 +376,17 @@ describe('ScriptTransformer', () => {
366
376
} ;
367
377
const scriptTransformer = new ScriptTransformer ( config ) ;
368
378
expect ( ( ) =>
369
- scriptTransformer . transformSource ( 'sample.js' , '' , false ) ,
379
+ scriptTransformer . transformSource ( 'sample.js' , '' , { instrument : false } ) ,
370
380
) . not . toThrow ( ) ;
371
381
} ) ;
372
382
373
383
it ( 'uses the supplied preprocessor' , ( ) => {
374
384
config = { ...config , transform : [ [ '\\.js$' , 'test_preprocessor' , { } ] ] } ;
375
385
const scriptTransformer = new ScriptTransformer ( config ) ;
376
386
const res1 = scriptTransformer . transform (
377
- '/fruits/banana.js' ,
387
+ filename ,
378
388
getCoverageOptions ( ) ,
389
+ mockFs [ filename ] ,
379
390
) ;
380
391
381
392
expect ( require ( 'test_preprocessor' ) . getCacheKey ) . toBeCalled ( ) ;
@@ -385,6 +396,7 @@ describe('ScriptTransformer', () => {
385
396
const res2 = scriptTransformer . transform (
386
397
'/node_modules/react.js' ,
387
398
getCoverageOptions ( ) ,
399
+ mockFs [ '/node_modules/react.js' ] ,
388
400
) ;
389
401
// ignores preprocessor
390
402
expect ( wrap ( res2 . code ) ) . toMatchSnapshot ( ) ;
@@ -401,12 +413,14 @@ describe('ScriptTransformer', () => {
401
413
const scriptTransformer = new ScriptTransformer ( config ) ;
402
414
403
415
const res1 = scriptTransformer . transform (
404
- '/fruits/banana.js' ,
416
+ filename ,
405
417
getCoverageOptions ( ) ,
418
+ mockFs [ filename ] ,
406
419
) ;
407
420
const res2 = scriptTransformer . transform (
408
421
'/styles/App.css' ,
409
422
getCoverageOptions ( ) ,
423
+ mockFs [ '/styles/App.css' ] ,
410
424
) ;
411
425
412
426
expect ( require ( 'test_preprocessor' ) . getCacheKey ) . toBeCalled ( ) ;
@@ -417,6 +431,7 @@ describe('ScriptTransformer', () => {
417
431
const res3 = scriptTransformer . transform (
418
432
'/node_modules/react.js' ,
419
433
getCoverageOptions ( ) ,
434
+ mockFs [ '/node_modules/react.js' ] ,
420
435
) ;
421
436
// ignores preprocessor
422
437
expect ( wrap ( res3 . code ) ) . toMatchSnapshot ( ) ;
@@ -440,8 +455,9 @@ describe('ScriptTransformer', () => {
440
455
} ) ;
441
456
442
457
const result = scriptTransformer . transform (
443
- '/fruits/banana.js' ,
458
+ filename ,
444
459
getCoverageOptions ( ) ,
460
+ mockFs [ filename ] ,
445
461
) ;
446
462
expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
447
463
const mapStr = JSON . stringify ( map ) ;
@@ -472,8 +488,9 @@ describe('ScriptTransformer', () => {
472
488
require ( 'preprocessor-with-sourcemaps' ) . process . mockReturnValue ( content ) ;
473
489
474
490
const result = scriptTransformer . transform (
475
- '/fruits/banana.js' ,
491
+ filename ,
476
492
getCoverageOptions ( ) ,
493
+ mockFs [ filename ] ,
477
494
) ;
478
495
expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
479
496
expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -508,8 +525,9 @@ describe('ScriptTransformer', () => {
508
525
require ( 'preprocessor-with-sourcemaps' ) . process . mockReturnValue ( content ) ;
509
526
510
527
const result = scriptTransformer . transform (
511
- '/fruits/banana.js' ,
528
+ filename ,
512
529
getCoverageOptions ( { collectCoverage : true } ) ,
530
+ mockFs [ filename ] ,
513
531
) ;
514
532
expect ( result . sourceMapPath ) . toBeNull ( ) ;
515
533
expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 1 ) ;
@@ -537,8 +555,9 @@ describe('ScriptTransformer', () => {
537
555
} ) ;
538
556
539
557
const result = scriptTransformer . transform (
540
- '/fruits/banana.js' ,
558
+ filename ,
541
559
getCoverageOptions ( ) ,
560
+ mockFs [ filename ] ,
542
561
) ;
543
562
expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
544
563
expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -565,8 +584,9 @@ describe('ScriptTransformer', () => {
565
584
} ) ;
566
585
567
586
const result = scriptTransformer . transform (
568
- '/fruits/banana.js' ,
587
+ filename ,
569
588
getCoverageOptions ( { collectCoverage : true } ) ,
589
+ mockFs [ filename ] ,
570
590
) ;
571
591
expect ( result . sourceMapPath ) . toBeFalsy ( ) ;
572
592
expect ( writeFileAtomic . sync ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -602,8 +622,9 @@ describe('ScriptTransformer', () => {
602
622
} ) ;
603
623
604
624
const result = scriptTransformer . transform (
605
- '/fruits/banana.js' ,
625
+ filename ,
606
626
getCoverageOptions ( { collectCoverage : true } ) ,
627
+ mockFs [ filename ] ,
607
628
) ;
608
629
expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
609
630
expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -638,8 +659,9 @@ describe('ScriptTransformer', () => {
638
659
} ) ;
639
660
640
661
const result = scriptTransformer . transform (
641
- '/fruits/banana.js' ,
662
+ filename ,
642
663
getCoverageOptions ( { collectCoverage : true } ) ,
664
+ mockFs [ filename ] ,
643
665
) ;
644
666
expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
645
667
expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -658,8 +680,9 @@ describe('ScriptTransformer', () => {
658
680
const scriptTransformer = new ScriptTransformer ( config ) ;
659
681
660
682
scriptTransformer . transform (
661
- '/fruits/banana.js' ,
683
+ filename ,
662
684
getCoverageOptions ( { collectCoverage : true } ) ,
685
+ mockFs [ filename ] ,
663
686
) ;
664
687
665
688
const { getCacheKey} = require ( 'test_preprocessor' ) ;
@@ -673,7 +696,11 @@ describe('ScriptTransformer', () => {
673
696
} ) ;
674
697
675
698
const scriptTransformer = new ScriptTransformer ( config ) ;
676
- scriptTransformer . transform ( '/fruits/banana.js' , { } ) ;
699
+ scriptTransformer . transform (
700
+ filename ,
701
+ getCoverageOptions ( ) ,
702
+ mockFs [ filename ] ,
703
+ ) ;
677
704
expect (
678
705
require ( 'configureable-preprocessor' ) . createTransformer ,
679
706
) . toHaveBeenCalledWith ( transformerConfig ) ;
@@ -685,7 +712,11 @@ describe('ScriptTransformer', () => {
685
712
transform : [ [ '\\.js$' , 'test_preprocessor' , { } ] ] ,
686
713
} ;
687
714
let scriptTransformer = new ScriptTransformer ( transformConfig ) ;
688
- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
715
+ scriptTransformer . transform (
716
+ filename ,
717
+ getCoverageOptions ( ) ,
718
+ mockFs [ filename ] ,
719
+ ) ;
689
720
690
721
const cachePath = getCachePath ( mockFs , config ) ;
691
722
expect ( writeFileAtomic . sync ) . toBeCalled ( ) ;
@@ -699,10 +730,13 @@ describe('ScriptTransformer', () => {
699
730
// Restore the cached fs
700
731
mockFs = mockFsCopy ;
701
732
scriptTransformer = new ScriptTransformer ( transformConfig ) ;
702
- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
733
+ scriptTransformer . transform (
734
+ filename ,
735
+ getCoverageOptions ( ) ,
736
+ mockFs [ filename ] ,
737
+ ) ;
703
738
704
- expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 2 ) ;
705
- expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana.js' , 'utf8' ) ;
739
+ expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
706
740
expect ( fs . readFileSync ) . toBeCalledWith ( cachePath , 'utf8' ) ;
707
741
expect ( writeFileAtomic . sync ) . not . toBeCalled ( ) ;
708
742
@@ -712,11 +746,13 @@ describe('ScriptTransformer', () => {
712
746
mockFs = mockFsCopy ;
713
747
transformConfig . cache = false ;
714
748
scriptTransformer = new ScriptTransformer ( transformConfig ) ;
715
- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
749
+ scriptTransformer . transform (
750
+ filename ,
751
+ getCoverageOptions ( ) ,
752
+ mockFs [ filename ] ,
753
+ ) ;
716
754
717
- expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
718
- expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana.js' , 'utf8' ) ;
719
- expect ( fs . readFileSync ) . not . toBeCalledWith ( cachePath , 'utf8' ) ;
755
+ expect ( fs . readFileSync ) . not . toHaveBeenCalled ( ) ;
720
756
expect ( writeFileAtomic . sync ) . toBeCalled ( ) ;
721
757
} ) ;
722
758
@@ -729,6 +765,7 @@ describe('ScriptTransformer', () => {
729
765
scriptTransformer . transform (
730
766
'/fruits/banana:colon.js' ,
731
767
getCoverageOptions ( ) ,
768
+ mockFs [ '/fruits/banana:colon.js' ] ,
732
769
) ;
733
770
734
771
const cachePath = getCachePath ( mockFs , config ) ;
@@ -743,7 +780,11 @@ describe('ScriptTransformer', () => {
743
780
// Restore the cached fs
744
781
mockFs = mockFsCopy ;
745
782
scriptTransformer = new ScriptTransformer ( transformConfig ) ;
746
- scriptTransformer . transform ( '/fruits/banana:colon.js' , { } ) ;
783
+ scriptTransformer . transform (
784
+ '/fruits/banana:colon.js' ,
785
+ getCoverageOptions ( ) ,
786
+ mockFs [ '/fruits/banana:colon.js' ] ,
787
+ ) ;
747
788
748
789
expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 2 ) ;
749
790
expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana:colon.js' , 'utf8' ) ;
@@ -757,20 +798,25 @@ describe('ScriptTransformer', () => {
757
798
transform : [ [ '\\.js$' , 'test_preprocessor' , { } ] ] ,
758
799
} ) ;
759
800
760
- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
801
+ scriptTransformer . transform (
802
+ filename ,
803
+ getCoverageOptions ( ) ,
804
+ mockFs [ filename ] ,
805
+ ) ;
761
806
762
807
const anotherScriptTransformer = new ScriptTransformer ( {
763
808
...config ,
764
809
transform : [ [ '\\.js$' , 'css-preprocessor' , { } ] ] ,
765
810
} ) ;
766
811
767
812
anotherScriptTransformer . transform (
768
- '/fruits/banana.js' ,
813
+ filename ,
769
814
getCoverageOptions ( ) ,
815
+ mockFs [ filename ] ,
770
816
) ;
771
817
772
818
expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 2 ) ;
773
- expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana.js' , 'utf8' ) ;
819
+ expect ( fs . readFileSync ) . toBeCalledWith ( filename , 'utf8' ) ;
774
820
} ) ;
775
821
776
822
it ( 'preload transformer when using `preloadTransformer`' , ( ) => {
@@ -781,9 +827,7 @@ describe('ScriptTransformer', () => {
781
827
782
828
expect ( Array . from ( scriptTransformer . _transformCache . entries ( ) ) ) . toEqual ( [ ] ) ;
783
829
784
- expect (
785
- scriptTransformer . preloadTransformer ( '/fruits/banana.js' ) ,
786
- ) . toBeUndefined ( ) ;
830
+ expect ( scriptTransformer . preloadTransformer ( filename ) ) . toBeUndefined ( ) ;
787
831
788
832
expect ( Array . from ( scriptTransformer . _transformCache . entries ( ) ) ) . toEqual ( [
789
833
[ 'test_preprocessor' , expect . any ( Object ) ] ,
0 commit comments