Skip to content

Commit

Permalink
chore: add more resolve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Musat authored and gabotechs committed Dec 24, 2022
1 parent d9e5a60 commit 7f0cef8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/js/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p *Parser) _uncachedResolvePath(unresolved string, dir string) (string, er
return absPath, fmt.Errorf("import '%s' was matched to path '%s' in tscofing's paths option, but the resolved path did not match an existing file", unresolved, pathOverride)
}
}
return absPath, nil
return absPath, fmt.Errorf("import '%s' cannot be resolved", unresolved)
}

func retrieveWithExt(absPath string) string {
Expand Down
61 changes: 55 additions & 6 deletions internal/js/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,41 @@ import (
"path"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/require"
)

const resolverTestFolder = ".resolve_test"

func TestParser_ResolvePath_IsCached(t *testing.T) {
a := require.New(t)
ctx := context.Background()
parser, err := MakeJsParser(resolverTestFolder)
a.NoError(err)

start := time.Now()
ctx, _, err = parser.ResolvePath(ctx, path.Join(resolverTestFolder, "src", "foo.ts"), resolverTestFolder)
a.NoError(err)
nonCached := time.Since(start)

start = time.Now()
_, _, err = parser.ResolvePath(ctx, path.Join(resolverTestFolder, "src", "foo.ts"), resolverTestFolder)
a.NoError(err)
cached := time.Since(start)

a.Greater(nonCached.Nanoseconds(), cached.Nanoseconds())
}

func TestParser_ResolvePath(t *testing.T) {
absPath, _ := filepath.Abs(resolverTestFolder)

tests := []struct {
Name string
Unresolved string
Cwd string
Resolved string
Name string
Unresolved string
Cwd string
Resolved string
ExpectedError string
}{
{
Name: "from relative",
Expand All @@ -44,6 +65,30 @@ func TestParser_ResolvePath(t *testing.T) {
Unresolved: "@/helpers/diff",
Resolved: path.Join(absPath, "src", "helpers", "diff.ts"),
},
{
Name: "Does not resolve invalid import",
Cwd: path.Join(resolverTestFolder, "src"),
Unresolved: "bar",
ExpectedError: "import 'bar' cannot be resolved",
},
{
Name: "Does not resolve invalid relative import",
Cwd: path.Join(resolverTestFolder, "src", "utils"),
Unresolved: "./foo",
ExpectedError: "could not perform relative import for './foo' because the file or dir was not found",
},
{
Name: "Does not resolve invalid relative import",
Cwd: resolverTestFolder,
Unresolved: path.Join("src", "utils", "foo"),
ExpectedError: "import 'src/utils/foo' cannot be resolved",
},
{
Name: "Does not resolve invalid path override import",
Cwd: path.Join(resolverTestFolder, "src"),
Unresolved: "@/helpers/bar",
ExpectedError: "import '@/helpers/bar' was matched to path '@/helpers/' in tscofing's paths option, but the resolved path did not match an existing file",
},
}

for _, tt := range tests {
Expand All @@ -52,8 +97,12 @@ func TestParser_ResolvePath(t *testing.T) {
parser, err := MakeJsParser(tt.Cwd)
a.NoError(err)
_, resolved, err := parser.ResolvePath(context.Background(), tt.Unresolved, tt.Cwd)
a.NoError(err)
a.Equal(tt.Resolved, resolved)
if tt.ExpectedError != "" {
a.ErrorContains(err, tt.ExpectedError)
} else {
a.NoError(err)
a.Equal(tt.Resolved, resolved)
}
})
}
}

0 comments on commit 7f0cef8

Please sign in to comment.