@@ -463,6 +463,52 @@ defmodule NPM.PackageResolverTest do
463463 # Helpers
464464 # ---------------------------------------------------------------------------
465465
466+ # ---------------------------------------------------------------------------
467+ # relative_import_path/3
468+ # ---------------------------------------------------------------------------
469+
470+ describe "relative_import_path/3" do
471+ test "sibling file gets ./ prefix" do
472+ assert PackageResolver . relative_import_path (
473+ "/app/src/index.js" ,
474+ "/app/src/app.js" ,
475+ "/app"
476+ ) == "./app.js"
477+ end
478+
479+ test "file in subdirectory" do
480+ assert PackageResolver . relative_import_path (
481+ "/app/src/index.js" ,
482+ "/app/src/utils/format.js" ,
483+ "/app"
484+ ) == "./utils/format.js"
485+ end
486+
487+ test "file in parent directory" do
488+ assert PackageResolver . relative_import_path (
489+ "/app/src/pages/home.js" ,
490+ "/app/src/utils/format.js" ,
491+ "/app"
492+ ) == "../utils/format.js"
493+ end
494+
495+ test "deeply nested upward traversal" do
496+ assert PackageResolver . relative_import_path (
497+ "/app/src/a/b/c/deep.js" ,
498+ "/app/src/lib/helper.js" ,
499+ "/app"
500+ ) == "../../../lib/helper.js"
501+ end
502+
503+ test "same directory different extensions" do
504+ assert PackageResolver . relative_import_path (
505+ "/app/components/button.tsx" ,
506+ "/app/components/button.module.css" ,
507+ "/app"
508+ ) == "./button.module.css"
509+ end
510+ end
511+
466512 defp write_pkg_json ( dir , data ) do
467513 File . write! ( Path . join ( dir , "package.json" ) , :json . encode ( data ) )
468514 end
0 commit comments