Skip to content

Commit

Permalink
chore: add caching 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 7f0cef8 commit fec7d05
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
22 changes: 22 additions & 0 deletions internal/js/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,34 @@ import (
"os"
"path"
"testing"
"time"

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

const exportsTestFolder = ".exports_test"

func TestParser_parseExports_IsCached(t *testing.T) {
a := require.New(t)
ctx := context.Background()
file := path.Join(exportsTestFolder, "src", "index.js")
parser, err := MakeJsParser(file)
a.NoError(err)

start := time.Now()
ctx, _, err = parser.parseExports(ctx, file)
a.NoError(err)
nonCached := time.Since(start)

start = time.Now()
_, _, err = parser.parseExports(ctx, file)
a.NoError(err)
cached := time.Since(start)

ratio := nonCached.Nanoseconds() / cached.Nanoseconds()
a.Greater(ratio, int64(10))
}

func TestParser_parseExports(t *testing.T) {
cwd, _ := os.Getwd()

Expand Down
24 changes: 23 additions & 1 deletion internal/js/imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,35 @@ import (
"os"
"path"
"testing"
"time"

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

const importsTestFolder = ".imports_test"

func TestFileInfo(t *testing.T) {
func TestParser_parseImports_IsCached(t *testing.T) {
a := require.New(t)
ctx := context.Background()
file := path.Join(importsTestFolder, "index.ts")
parser, err := MakeJsParser(file)
a.NoError(err)

start := time.Now()
ctx, _, err = parser.parseImports(ctx, file)
a.NoError(err)
nonCached := time.Since(start)

start = time.Now()
_, _, err = parser.parseImports(ctx, file)
a.NoError(err)
cached := time.Since(start)

ratio := nonCached.Nanoseconds() / cached.Nanoseconds()
a.Greater(ratio, int64(10))
}

func TestParser_parseImports(t *testing.T) {
wd, _ := os.Getwd()

tests := []struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/js/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func TestParser_ResolvePath_IsCached(t *testing.T) {
a.NoError(err)
cached := time.Since(start)

a.Greater(nonCached.Nanoseconds(), cached.Nanoseconds())
ratio := nonCached.Nanoseconds() / cached.Nanoseconds()
a.Greater(ratio, int64(5))
}

func TestParser_ResolvePath(t *testing.T) {
Expand Down

0 comments on commit fec7d05

Please sign in to comment.