This commit is contained in:
2026-01-27 20:04:36 -05:00
parent c3a16c9e92
commit f9d3753806
13 changed files with 247 additions and 352 deletions

View File

@@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dop251/goja"
"modernc.org/quickjs"
)
type TestArgs struct {
@@ -36,15 +36,18 @@ func TestAsyncBuiltinResolution(t *testing.T) {
return "test-result", nil
})
vm := goja.New()
vm, err := quickjs.NewVM()
require.NoError(t, err)
defer func() {
_ = vm.Close()
}()
vm.SetCanBlock(true)
RegisterBuiltins(vm)
result, err := vm.RunString(`resolveTest({field1: "hello"})`)
result, err := vm.Eval(`resolveTest({field1: "hello"})`, quickjs.EvalGlobal)
require.NoError(t, err)
promise, ok := result.Export().(*goja.Promise)
require.True(t, ok, "should return a Promise")
assert.NotNil(t, promise)
assert.NotNil(t, result)
}
func TestAsyncBuiltinRejection(t *testing.T) {
@@ -52,15 +55,18 @@ func TestAsyncBuiltinRejection(t *testing.T) {
return "", assert.AnError
})
vm := goja.New()
vm, err := quickjs.NewVM()
require.NoError(t, err)
defer func() {
_ = vm.Close()
}()
vm.SetCanBlock(true)
RegisterBuiltins(vm)
result, err := vm.RunString(`rejectTest({field1: "hello"})`)
result, err := vm.Eval(`rejectTest({field1: "hello"})`, quickjs.EvalGlobal)
require.NoError(t, err)
promise, ok := result.Export().(*goja.Promise)
require.True(t, ok, "should return a Promise")
assert.NotNil(t, promise)
assert.NotNil(t, result)
}
func TestNonPromise(t *testing.T) {
@@ -68,10 +74,22 @@ func TestNonPromise(t *testing.T) {
return "sync-result", nil
})
vm := goja.New()
vm, err := quickjs.NewVM()
require.NoError(t, err)
defer func() {
_ = vm.Close()
}()
vm.SetCanBlock(true)
RegisterBuiltins(vm)
result, err := vm.RunString(`nonPromiseTest({field1: "hello"})`)
result, err := vm.Eval(`nonPromiseTest({field1: "hello"})`, quickjs.EvalGlobal)
require.NoError(t, err)
assert.Equal(t, "sync-result", result.Export())
if obj, ok := result.(*quickjs.Object); ok {
var arr []any
if err := obj.Into(&arr); err == nil && len(arr) > 0 {
assert.Equal(t, "sync-result", arr[0])
}
}
}