@@ -597,4 +597,95 @@ describe('module-resolver', () => {
597597 } ) ;
598598 } ) ;
599599 } ) ;
600+
601+ describe ( 'packagejson' , ( ) => {
602+ const transformerOpts = {
603+ babelrc : false ,
604+ plugins : [
605+ [ plugin , {
606+ root : './src' ,
607+ alias : {
608+ test : './test' ,
609+ } ,
610+ cwd : 'packagejson' ,
611+ } ] ,
612+ ] ,
613+ filename : './test/testproject/src/app.js' ,
614+ } ;
615+
616+ it ( 'should resolve the sub file path' , ( ) => {
617+ testWithImport (
618+ 'components/Root' ,
619+ './components/Root' ,
620+ transformerOpts ,
621+ ) ;
622+ } ) ;
623+
624+ it ( 'should alias the sub file path' , ( ) => {
625+ testWithImport (
626+ 'test/tools' ,
627+ '../test/tools' ,
628+ transformerOpts ,
629+ ) ;
630+ } ) ;
631+
632+ describe ( 'unknown filename' , ( ) => {
633+ const unknownFileTransformerOpts = {
634+ babelrc : false ,
635+ plugins : [
636+ [ plugin , {
637+ root : './src' ,
638+ cwd : 'packagejson' ,
639+ } ] ,
640+ ] ,
641+ } ;
642+ const cachedCwd = process . cwd ( ) ;
643+ const pkgJsonDir = 'test/testproject' ;
644+
645+ beforeEach ( ( ) => {
646+ process . chdir ( pkgJsonDir ) ;
647+ } ) ;
648+
649+ afterEach ( ( ) => {
650+ process . chdir ( cachedCwd ) ;
651+ } ) ;
652+
653+ it ( 'should resolve the sub file path' , ( ) => {
654+ testWithImport (
655+ 'components/Root' ,
656+ './src/components/Root' ,
657+ unknownFileTransformerOpts ,
658+ ) ;
659+ } ) ;
660+ } ) ;
661+
662+ describe ( 'missing packagejson in path (uses cwd)' , ( ) => {
663+ jest . mock ( 'pkg-up' , ( ) => ( {
664+ sync : function pkgUpSync ( ) {
665+ return null ;
666+ } ,
667+ } ) ) ;
668+ jest . resetModules ( ) ;
669+ const pluginWithMock = require . requireActual ( '../src' ) . default ;
670+
671+ const missingPkgJsonConfigTransformerOpts = {
672+ babelrc : false ,
673+ plugins : [
674+ [ pluginWithMock , {
675+ root : '.' ,
676+ cwd : 'packagejson' ,
677+ } ] ,
678+ ] ,
679+ filename : './test/testproject/src/app.js' ,
680+ } ;
681+
682+ it ( 'should resolve the sub file path' , ( ) => {
683+ testWithImport (
684+ 'test/testproject/src/components/Root' ,
685+ './components/Root' ,
686+ missingPkgJsonConfigTransformerOpts ,
687+ ) ;
688+ } ) ;
689+ } ) ;
690+ } ) ;
600691} ) ;
0 commit comments