@@ -151,6 +151,8 @@ let mockFs: Record<Config.Path, string>;
151151let object : < T > ( input : T ) => T ;
152152let writeFileAtomic : typeof import ( 'write-file-atomic' ) ;
153153
154+ const filename = '/fruits/banana.js' ;
155+
154156jest . mock ( 'write-file-atomic' , ( ) => ( {
155157 sync : jest . fn ( ) . mockImplementation ( ( filePath , data ) => {
156158 mockFs [ filePath ] = data ;
@@ -228,20 +230,18 @@ describe('ScriptTransformer', () => {
228230 it ( 'transforms a file properly' , ( ) => {
229231 const scriptTransformer = new ScriptTransformer ( config ) ;
230232 const transformedBananaWithCoverage = scriptTransformer . transform (
231- '/fruits/banana.js' ,
233+ filename ,
232234 getCoverageOptions ( { collectCoverage : true } ) ,
235+ mockFs [ filename ] ,
233236 ) ;
234237
235238 expect ( wrap ( transformedBananaWithCoverage . code ) ) . toMatchSnapshot ( ) ;
236239
237- // no-cache case
238- expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
239- expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana.js' , 'utf8' ) ;
240-
241240 // in-memory cache
242241 const transformedBananaWithCoverageAgain = scriptTransformer . transform (
243- '/fruits/banana.js' ,
242+ filename ,
244243 getCoverageOptions ( { collectCoverage : true } ) ,
244+ mockFs [ filename ] ,
245245 ) ;
246246 expect ( transformedBananaWithCoverageAgain ) . toBe (
247247 transformedBananaWithCoverage ,
@@ -250,6 +250,7 @@ describe('ScriptTransformer', () => {
250250 const transformedKiwiWithCoverage = scriptTransformer . transform (
251251 '/fruits/kiwi.js' ,
252252 getCoverageOptions ( { collectCoverage : true } ) ,
253+ mockFs [ '/fruits/kiwi.js' ] ,
253254 ) ;
254255 expect ( wrap ( transformedKiwiWithCoverage . code ) ) . toMatchSnapshot ( ) ;
255256
@@ -262,6 +263,7 @@ describe('ScriptTransformer', () => {
262263 const transformedKiwiWithoutCoverage = scriptTransformer . transform (
263264 '/fruits/kiwi.js' ,
264265 getCoverageOptions ( { collectCoverage : false } ) ,
266+ mockFs [ '/fruits/kiwi.js' ] ,
265267 ) ;
266268
267269 expect ( transformedKiwiWithoutCoverage . code ) . not . toEqual (
@@ -299,7 +301,7 @@ describe('ScriptTransformer', () => {
299301 const scriptTransformer = new ScriptTransformer ( config ) ;
300302
301303 const incorrectReturnValues = [
302- [ undefined , '/fruits/banana.js' ] ,
304+ [ undefined , filename ] ,
303305 [ { a : 'a' } , '/fruits/kiwi.js' ] ,
304306 [ [ ] , '/fruits/grapefruit.js' ] ,
305307 ] ;
@@ -310,12 +312,16 @@ describe('ScriptTransformer', () => {
310312 returnValue ,
311313 ) ;
312314 expect ( ( ) =>
313- scriptTransformer . transform ( filePath , getCoverageOptions ( ) ) ,
315+ scriptTransformer . transform (
316+ filePath ,
317+ getCoverageOptions ( ) ,
318+ mockFs [ filename ] ,
319+ ) ,
314320 ) . toThrow ( 'must return a string' ) ;
315321 } ) ;
316322
317323 const correctReturnValues = [
318- [ 'code' , '/fruits/banana.js' ] ,
324+ [ 'code' , filename ] ,
319325 [ { code : 'code' } , '/fruits/kiwi.js' ] ,
320326 ] ;
321327
@@ -325,7 +331,11 @@ describe('ScriptTransformer', () => {
325331 returnValue ,
326332 ) ;
327333 expect ( ( ) =>
328- scriptTransformer . transform ( filePath , getCoverageOptions ( ) ) ,
334+ scriptTransformer . transform (
335+ filePath ,
336+ getCoverageOptions ( ) ,
337+ mockFs [ filename ] ,
338+ ) ,
329339 ) . not . toThrow ( ) ;
330340 } ) ;
331341 } ,
@@ -338,7 +348,7 @@ describe('ScriptTransformer', () => {
338348 } ;
339349 const scriptTransformer = new ScriptTransformer ( config ) ;
340350 expect ( ( ) =>
341- scriptTransformer . transformSource ( 'sample.js' , '' , false ) ,
351+ scriptTransformer . transformSource ( 'sample.js' , '' , { instrument : false } ) ,
342352 ) . toThrow ( 'Jest: a transform must export a `process` function.' ) ;
343353 } ) ;
344354
@@ -355,7 +365,7 @@ describe('ScriptTransformer', () => {
355365 } ;
356366 const scriptTransformer = new ScriptTransformer ( config ) ;
357367 expect ( ( ) =>
358- scriptTransformer . transformSource ( 'sample.js' , '' , false ) ,
368+ scriptTransformer . transformSource ( 'sample.js' , '' , { instrument : false } ) ,
359369 ) . toThrow ( 'Jest: a transform must export a `process` function.' ) ;
360370 } ) ;
361371
@@ -366,16 +376,17 @@ describe('ScriptTransformer', () => {
366376 } ;
367377 const scriptTransformer = new ScriptTransformer ( config ) ;
368378 expect ( ( ) =>
369- scriptTransformer . transformSource ( 'sample.js' , '' , false ) ,
379+ scriptTransformer . transformSource ( 'sample.js' , '' , { instrument : false } ) ,
370380 ) . not . toThrow ( ) ;
371381 } ) ;
372382
373383 it ( 'uses the supplied preprocessor' , ( ) => {
374384 config = { ...config , transform : [ [ '\\.js$' , 'test_preprocessor' , { } ] ] } ;
375385 const scriptTransformer = new ScriptTransformer ( config ) ;
376386 const res1 = scriptTransformer . transform (
377- '/fruits/banana.js' ,
387+ filename ,
378388 getCoverageOptions ( ) ,
389+ mockFs [ filename ] ,
379390 ) ;
380391
381392 expect ( require ( 'test_preprocessor' ) . getCacheKey ) . toBeCalled ( ) ;
@@ -385,6 +396,7 @@ describe('ScriptTransformer', () => {
385396 const res2 = scriptTransformer . transform (
386397 '/node_modules/react.js' ,
387398 getCoverageOptions ( ) ,
399+ mockFs [ '/node_modules/react.js' ] ,
388400 ) ;
389401 // ignores preprocessor
390402 expect ( wrap ( res2 . code ) ) . toMatchSnapshot ( ) ;
@@ -401,12 +413,14 @@ describe('ScriptTransformer', () => {
401413 const scriptTransformer = new ScriptTransformer ( config ) ;
402414
403415 const res1 = scriptTransformer . transform (
404- '/fruits/banana.js' ,
416+ filename ,
405417 getCoverageOptions ( ) ,
418+ mockFs [ filename ] ,
406419 ) ;
407420 const res2 = scriptTransformer . transform (
408421 '/styles/App.css' ,
409422 getCoverageOptions ( ) ,
423+ mockFs [ '/styles/App.css' ] ,
410424 ) ;
411425
412426 expect ( require ( 'test_preprocessor' ) . getCacheKey ) . toBeCalled ( ) ;
@@ -417,6 +431,7 @@ describe('ScriptTransformer', () => {
417431 const res3 = scriptTransformer . transform (
418432 '/node_modules/react.js' ,
419433 getCoverageOptions ( ) ,
434+ mockFs [ '/node_modules/react.js' ] ,
420435 ) ;
421436 // ignores preprocessor
422437 expect ( wrap ( res3 . code ) ) . toMatchSnapshot ( ) ;
@@ -440,8 +455,9 @@ describe('ScriptTransformer', () => {
440455 } ) ;
441456
442457 const result = scriptTransformer . transform (
443- '/fruits/banana.js' ,
458+ filename ,
444459 getCoverageOptions ( ) ,
460+ mockFs [ filename ] ,
445461 ) ;
446462 expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
447463 const mapStr = JSON . stringify ( map ) ;
@@ -472,8 +488,9 @@ describe('ScriptTransformer', () => {
472488 require ( 'preprocessor-with-sourcemaps' ) . process . mockReturnValue ( content ) ;
473489
474490 const result = scriptTransformer . transform (
475- '/fruits/banana.js' ,
491+ filename ,
476492 getCoverageOptions ( ) ,
493+ mockFs [ filename ] ,
477494 ) ;
478495 expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
479496 expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -508,8 +525,9 @@ describe('ScriptTransformer', () => {
508525 require ( 'preprocessor-with-sourcemaps' ) . process . mockReturnValue ( content ) ;
509526
510527 const result = scriptTransformer . transform (
511- '/fruits/banana.js' ,
528+ filename ,
512529 getCoverageOptions ( { collectCoverage : true } ) ,
530+ mockFs [ filename ] ,
513531 ) ;
514532 expect ( result . sourceMapPath ) . toBeNull ( ) ;
515533 expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 1 ) ;
@@ -537,8 +555,9 @@ describe('ScriptTransformer', () => {
537555 } ) ;
538556
539557 const result = scriptTransformer . transform (
540- '/fruits/banana.js' ,
558+ filename ,
541559 getCoverageOptions ( ) ,
560+ mockFs [ filename ] ,
542561 ) ;
543562 expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
544563 expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -565,8 +584,9 @@ describe('ScriptTransformer', () => {
565584 } ) ;
566585
567586 const result = scriptTransformer . transform (
568- '/fruits/banana.js' ,
587+ filename ,
569588 getCoverageOptions ( { collectCoverage : true } ) ,
589+ mockFs [ filename ] ,
570590 ) ;
571591 expect ( result . sourceMapPath ) . toBeFalsy ( ) ;
572592 expect ( writeFileAtomic . sync ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -602,8 +622,9 @@ describe('ScriptTransformer', () => {
602622 } ) ;
603623
604624 const result = scriptTransformer . transform (
605- '/fruits/banana.js' ,
625+ filename ,
606626 getCoverageOptions ( { collectCoverage : true } ) ,
627+ mockFs [ filename ] ,
607628 ) ;
608629 expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
609630 expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -638,8 +659,9 @@ describe('ScriptTransformer', () => {
638659 } ) ;
639660
640661 const result = scriptTransformer . transform (
641- '/fruits/banana.js' ,
662+ filename ,
642663 getCoverageOptions ( { collectCoverage : true } ) ,
664+ mockFs [ filename ] ,
643665 ) ;
644666 expect ( result . sourceMapPath ) . toEqual ( expect . any ( String ) ) ;
645667 expect ( writeFileAtomic . sync ) . toBeCalledTimes ( 2 ) ;
@@ -658,8 +680,9 @@ describe('ScriptTransformer', () => {
658680 const scriptTransformer = new ScriptTransformer ( config ) ;
659681
660682 scriptTransformer . transform (
661- '/fruits/banana.js' ,
683+ filename ,
662684 getCoverageOptions ( { collectCoverage : true } ) ,
685+ mockFs [ filename ] ,
663686 ) ;
664687
665688 const { getCacheKey} = require ( 'test_preprocessor' ) ;
@@ -673,7 +696,11 @@ describe('ScriptTransformer', () => {
673696 } ) ;
674697
675698 const scriptTransformer = new ScriptTransformer ( config ) ;
676- scriptTransformer . transform ( '/fruits/banana.js' , { } ) ;
699+ scriptTransformer . transform (
700+ filename ,
701+ getCoverageOptions ( ) ,
702+ mockFs [ filename ] ,
703+ ) ;
677704 expect (
678705 require ( 'configureable-preprocessor' ) . createTransformer ,
679706 ) . toHaveBeenCalledWith ( transformerConfig ) ;
@@ -685,7 +712,11 @@ describe('ScriptTransformer', () => {
685712 transform : [ [ '\\.js$' , 'test_preprocessor' , { } ] ] ,
686713 } ;
687714 let scriptTransformer = new ScriptTransformer ( transformConfig ) ;
688- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
715+ scriptTransformer . transform (
716+ filename ,
717+ getCoverageOptions ( ) ,
718+ mockFs [ filename ] ,
719+ ) ;
689720
690721 const cachePath = getCachePath ( mockFs , config ) ;
691722 expect ( writeFileAtomic . sync ) . toBeCalled ( ) ;
@@ -699,10 +730,13 @@ describe('ScriptTransformer', () => {
699730 // Restore the cached fs
700731 mockFs = mockFsCopy ;
701732 scriptTransformer = new ScriptTransformer ( transformConfig ) ;
702- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
733+ scriptTransformer . transform (
734+ filename ,
735+ getCoverageOptions ( ) ,
736+ mockFs [ filename ] ,
737+ ) ;
703738
704- expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 2 ) ;
705- expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana.js' , 'utf8' ) ;
739+ expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 1 ) ;
706740 expect ( fs . readFileSync ) . toBeCalledWith ( cachePath , 'utf8' ) ;
707741 expect ( writeFileAtomic . sync ) . not . toBeCalled ( ) ;
708742
@@ -712,11 +746,13 @@ describe('ScriptTransformer', () => {
712746 mockFs = mockFsCopy ;
713747 transformConfig . cache = false ;
714748 scriptTransformer = new ScriptTransformer ( transformConfig ) ;
715- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
749+ scriptTransformer . transform (
750+ filename ,
751+ getCoverageOptions ( ) ,
752+ mockFs [ filename ] ,
753+ ) ;
716754
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 ( ) ;
720756 expect ( writeFileAtomic . sync ) . toBeCalled ( ) ;
721757 } ) ;
722758
@@ -729,6 +765,7 @@ describe('ScriptTransformer', () => {
729765 scriptTransformer . transform (
730766 '/fruits/banana:colon.js' ,
731767 getCoverageOptions ( ) ,
768+ mockFs [ '/fruits/banana:colon.js' ] ,
732769 ) ;
733770
734771 const cachePath = getCachePath ( mockFs , config ) ;
@@ -743,7 +780,11 @@ describe('ScriptTransformer', () => {
743780 // Restore the cached fs
744781 mockFs = mockFsCopy ;
745782 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+ ) ;
747788
748789 expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 2 ) ;
749790 expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana:colon.js' , 'utf8' ) ;
@@ -757,20 +798,25 @@ describe('ScriptTransformer', () => {
757798 transform : [ [ '\\.js$' , 'test_preprocessor' , { } ] ] ,
758799 } ) ;
759800
760- scriptTransformer . transform ( '/fruits/banana.js' , getCoverageOptions ( ) ) ;
801+ scriptTransformer . transform (
802+ filename ,
803+ getCoverageOptions ( ) ,
804+ mockFs [ filename ] ,
805+ ) ;
761806
762807 const anotherScriptTransformer = new ScriptTransformer ( {
763808 ...config ,
764809 transform : [ [ '\\.js$' , 'css-preprocessor' , { } ] ] ,
765810 } ) ;
766811
767812 anotherScriptTransformer . transform (
768- '/fruits/banana.js' ,
813+ filename ,
769814 getCoverageOptions ( ) ,
815+ mockFs [ filename ] ,
770816 ) ;
771817
772818 expect ( fs . readFileSync ) . toHaveBeenCalledTimes ( 2 ) ;
773- expect ( fs . readFileSync ) . toBeCalledWith ( '/fruits/banana.js' , 'utf8' ) ;
819+ expect ( fs . readFileSync ) . toBeCalledWith ( filename , 'utf8' ) ;
774820 } ) ;
775821
776822 it ( 'preload transformer when using `preloadTransformer`' , ( ) => {
@@ -781,9 +827,7 @@ describe('ScriptTransformer', () => {
781827
782828 expect ( Array . from ( scriptTransformer . _transformCache . entries ( ) ) ) . toEqual ( [ ] ) ;
783829
784- expect (
785- scriptTransformer . preloadTransformer ( '/fruits/banana.js' ) ,
786- ) . toBeUndefined ( ) ;
830+ expect ( scriptTransformer . preloadTransformer ( filename ) ) . toBeUndefined ( ) ;
787831
788832 expect ( Array . from ( scriptTransformer . _transformCache . entries ( ) ) ) . toEqual ( [
789833 [ 'test_preprocessor' , expect . any ( Object ) ] ,
0 commit comments