@@ -6,29 +6,12 @@ jest.mock('jscodeshift/src/Runner', () => ({
66// @ts -ignore
77import * as jscodeshift from 'jscodeshift/src/Runner' ;
88import { PluginManager } from 'live-plugin-manager' ;
9+
910import main from './main' ;
1011
1112const mockPath = 'src/pages/home-page/' ;
1213
1314describe ( 'main' , ( ) => {
14- beforeEach ( ( ) => {
15- ( PluginManager as jest . Mock ) . mockReturnValue ( {
16- install : ( ) => Promise . resolve ( undefined ) ,
17- require : ( codemodName : string ) => ( {
18- transforms : {
19- '18.0.0' : `${ codemodName } /path/to/18.js` ,
20- '19.0.0' : `${ codemodName } /path/to/19.js` ,
21- '20.0.0' : `${ codemodName } /path/to/20.js` ,
22- } ,
23- presets : {
24- 'update-formatting' : `${ codemodName } /path/to/update-formatting.js` ,
25- 'update-imports' : `${ codemodName } /path/to/update-imports.js` ,
26- } ,
27- } ) ,
28- uninstallAll : ( ) => Promise . resolve ( ) ,
29- } ) ;
30- } ) ;
31-
3215 afterEach ( ( ) => {
3316 jest . resetAllMocks ( ) ;
3417 } ) ;
@@ -134,6 +117,26 @@ describe('main', () => {
134117 } ) ;
135118
136119 describe ( 'when running transforms with the -p flag' , ( ) => {
120+ beforeEach ( ( ) => {
121+ ( PluginManager as jest . Mock ) . mockImplementation ( ( ) => ( {
122+ install : jest . fn ( ) . mockResolvedValue ( undefined ) ,
123+ require : jest . fn ( ) . mockImplementation ( ( codemodName : string ) => {
124+ if ( ! codemodName . startsWith ( '@codeshift' ) ) {
125+ throw new Error ( 'Attempted to fetch codemod from npm' ) ;
126+ }
127+
128+ return {
129+ transforms : {
130+ '18.0.0' : `${ codemodName } /path/to/18.js` ,
131+ '19.0.0' : `${ codemodName } /path/to/19.js` ,
132+ '20.0.0' : `${ codemodName } /path/to/20.js` ,
133+ } ,
134+ } ;
135+ } ) ,
136+ uninstallAll : jest . fn ( ) . mockResolvedValue ( undefined ) ,
137+ } ) ) ;
138+ } ) ;
139+
137140 it ( 'should run package transform for single version' , async ( ) => {
138141 await main ( [ mockPath ] , {
139142 packages : 'mylib@18.0.0' ,
@@ -234,6 +237,7 @@ describe('main', () => {
234237 expect . any ( Object ) ,
235238 ) ;
236239 } ) ;
240+
237241 it ( 'should run multiple transforms of the same package' , async ( ) => {
238242 await main ( [ mockPath ] , {
239243 packages : '@myscope/mylib@20.0.0@19.0.0' ,
@@ -374,6 +378,25 @@ describe('main', () => {
374378 } ) ;
375379
376380 describe ( 'when running presets with the -p flag' , ( ) => {
381+ beforeEach ( ( ) => {
382+ ( PluginManager as jest . Mock ) . mockImplementation ( ( ) => ( {
383+ install : jest . fn ( ) . mockResolvedValue ( undefined ) ,
384+ require : jest . fn ( ) . mockImplementation ( ( codemodName : string ) => {
385+ if ( ! codemodName . startsWith ( '@codeshift' ) ) {
386+ throw new Error ( 'Attempted to fetch codemod from npm' ) ;
387+ }
388+
389+ return {
390+ presets : {
391+ 'update-formatting' : `${ codemodName } /path/to/update-formatting.js` ,
392+ 'update-imports' : `${ codemodName } /path/to/update-imports.js` ,
393+ } ,
394+ } ;
395+ } ) ,
396+ uninstallAll : jest . fn ( ) . mockResolvedValue ( undefined ) ,
397+ } ) ) ;
398+ } ) ;
399+
377400 it ( 'should run single preset' , async ( ) => {
378401 await main ( [ mockPath ] , {
379402 packages : 'mylib#update-formatting' ,
@@ -508,18 +531,71 @@ describe('main', () => {
508531 } ) ;
509532 } ) ;
510533
534+ describe ( 'when running transforms from NPM with the -p flag' , ( ) => {
535+ beforeEach ( ( ) => {
536+ ( PluginManager as jest . Mock ) . mockImplementation ( ( ) => ( {
537+ install : jest . fn ( ) . mockResolvedValue ( undefined ) ,
538+ require : jest . fn ( ) . mockImplementation ( ( codemodName : string ) => {
539+ if ( codemodName . startsWith ( '@codeshift' ) ) {
540+ throw new Error ( 'Attempted to fetch codemod from community folder' ) ;
541+ }
542+
543+ return {
544+ transforms : {
545+ '18.0.0' : `${ codemodName } /path/to/18.js` ,
546+ } ,
547+ presets : {
548+ 'update-formatting' : `${ codemodName } /path/to/update-formatting.js` ,
549+ } ,
550+ } ;
551+ } ) ,
552+ uninstallAll : jest . fn ( ) . mockResolvedValue ( undefined ) ,
553+ } ) ) ;
554+ } ) ;
555+
556+ it ( 'should run package transform for single version' , async ( ) => {
557+ await main ( [ mockPath ] , {
558+ packages : 'mylib@18.0.0' ,
559+ parser : 'babel' ,
560+ extensions : 'js' ,
561+ } ) ;
562+
563+ expect ( jscodeshift . run ) . toHaveBeenCalledTimes ( 1 ) ;
564+ expect ( jscodeshift . run ) . toHaveBeenCalledWith (
565+ 'mylib/path/to/18.js' ,
566+ expect . arrayContaining ( [ mockPath ] ) ,
567+ expect . anything ( ) ,
568+ ) ;
569+ } ) ;
570+
571+ it ( 'should run single preset' , async ( ) => {
572+ await main ( [ mockPath ] , {
573+ packages : 'mylib#update-formatting' ,
574+ parser : 'babel' ,
575+ extensions : 'js' ,
576+ } ) ;
577+
578+ expect ( jscodeshift . run ) . toHaveBeenCalledTimes ( 1 ) ;
579+ expect ( jscodeshift . run ) . toHaveBeenCalledWith (
580+ 'mylib/path/to/update-formatting.js' ,
581+ expect . arrayContaining ( [ mockPath ] ) ,
582+ expect . anything ( ) ,
583+ ) ;
584+ } ) ;
585+ } ) ;
586+
511587 describe ( 'when reading configs using non-cjs exports' , ( ) => {
512588 it ( 'should read configs exported with export default' , async ( ) => {
513589 ( PluginManager as jest . Mock ) . mockReturnValue ( {
514590 install : ( ) => Promise . resolve ( undefined ) ,
515591 // @ts -ignore
516- require : ( codemodName : string ) => ( {
592+ require : jest . fn ( ) . mockImplementationOnce ( ( codemodName : string ) => ( {
517593 default : {
518594 transforms : {
519595 '18.0.0' : `${ codemodName } /path/to/18.js` ,
520596 } ,
521597 } ,
522- } ) ,
598+ } ) ) ,
523599 uninstallAll : ( ) => Promise . resolve ( ) ,
524600 } ) ;
525601
0 commit comments