diff --git a/js/modules/k6/k6.go b/js/modules/k6/k6.go index 57d525c3c871..a442fe54a6e8 100644 --- a/js/modules/k6/k6.go +++ b/js/modules/k6/k6.go @@ -22,6 +22,8 @@ var ( ErrCheckInInitContext = common.NewInitContextError("Using check() in the init context is not supported") ) +const asyncFunctionNotSupportedMsg = "Async Function not supported: Come up with a message here" + type ( // RootModule is the global module instance that will create module // instances for each VU. @@ -85,16 +87,24 @@ func (mi *K6) RandomSeed(seed int64) { } // Group wraps a function call and executes it within the provided group name. -func (mi *K6) Group(name string, fn goja.Callable) (goja.Value, error) { +func (mi *K6) Group(name string, val goja.Value) (goja.Value, error) { state := mi.vu.State() if state == nil { return nil, ErrGroupInInitContext } - if fn == nil { + if val == nil || goja.IsNull(val) { return nil, errors.New("group() requires a callback as a second argument") } - + fn, ok := goja.AssertFunction(val) + if !ok { + return nil, errors.New("group() requires a callback as a second argument") + } + rt := mi.vu.Runtime() + o := val.ToObject(rt) + if o.ClassName() == "AsyncFunction" { + return goja.Undefined(), errors.New(asyncFunctionNotSupportedMsg) + } g, err := state.Group.Group(name) if err != nil { return goja.Undefined(), err @@ -138,6 +148,7 @@ func (mi *K6) Group(name string, fn goja.Callable) (goja.Value, error) { } // Check will emit check metrics for the provided checks. +// //nolint:cyclop func (mi *K6) Check(arg0, checks goja.Value, extras ...goja.Value) (bool, error) { state := mi.vu.State() diff --git a/js/modules/k6/k6_test.go b/js/modules/k6/k6_test.go index 7bf504ab0ba6..0f58854f299d 100644 --- a/js/modules/k6/k6_test.go +++ b/js/modules/k6/k6_test.go @@ -186,6 +186,20 @@ func TestGroup(t *testing.T) { _, err := rt.RunString(`k6.group("::", function() { throw new Error("nooo") })`) assert.Contains(t, err.Error(), "group and check names may not contain '::'") }) + + t.Run("async function", func(t *testing.T) { + t.Parallel() + rt, _, _ := setupGroupTest() + _, err := rt.RunString(`k6.group("something", async function() { })`) + assert.ErrorContains(t, err, asyncFunctionNotSupportedMsg) + }) + + t.Run("async lambda", func(t *testing.T) { + t.Parallel() + rt, _, _ := setupGroupTest() + _, err := rt.RunString(`k6.group("something", async () => { })`) + assert.ErrorContains(t, err, asyncFunctionNotSupportedMsg) + }) } func checkTestRuntime(t testing.TB) (*goja.Runtime, chan metrics.SampleContainer, *metrics.BuiltinMetrics) {