Skip to content

Commit

Permalink
test: increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
szkiba committed May 14, 2024
1 parent c5b1c24 commit 2758044
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/dop251/goja"
"github.com/dop251/goja/parser"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -158,6 +159,34 @@ func TestCompile(t *testing.T) {
assert.Contains(t, err.Error(), `SyntaxError: script.js: Unexpected token (1:3)
> 1 | 1+(=>2)()`)
})

t.Run("enhanced Invalid", func(t *testing.T) {
t.Parallel()
c := New(testutils.NewLogger(t))
src := `1+(function() { return 2; )()`
c.Options.CompatibilityMode = lib.CompatibilityModeEnhanced
_, _, err := c.Compile(src, "script.js", false)
assert.IsType(t, &parser.Error{}, err)
assert.Contains(t, err.Error(), `script.js: Line 1:26 Unexpected ")"`)
})
t.Run("enhanced", func(t *testing.T) {
t.Parallel()
c := New(testutils.NewLogger(t))
c.Options.CompatibilityMode = lib.CompatibilityModeEnhanced
pgm, code, err := c.Compile(`import "something"`, "script.js", true)
require.NoError(t, err)
assert.Equal(t, `var import_something = require("something");
`, code)
rt := goja.New()
var requireCalled bool
require.NoError(t, rt.Set("require", func(s string) {
assert.Equal(t, "something", s)
requireCalled = true
}))
_, err = rt.RunProgram(pgm)
require.NoError(t, err)
require.True(t, requireCalled)
})
}

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

0 comments on commit 2758044

Please sign in to comment.