diff --git a/js/modules/k6/execution/execution.go b/js/modules/k6/execution/execution.go index d014699b8bc7..001cc403cd1a 100644 --- a/js/modules/k6/execution/execution.go +++ b/js/modules/k6/execution/execution.go @@ -127,12 +127,15 @@ func (mi *ModuleInstance) newScenarioInfo() (*goja.Object, error) { return newInfoObj(rt, si) } +//nolint:lll,gochecknoglobals +var instanceInfoInitContextErr = common.NewInitContextError("getting instance information in the init context is not supported") + // newInstanceInfo returns a goja.Object with property accessors to retrieve // information about the local instance stats. func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) { es := lib.GetExecutionState(mi.vu.Context()) if es == nil { - return nil, errors.New("getting instance information in the init context is not supported") + return nil, instanceInfoInitContextErr } rt := mi.vu.Runtime() @@ -157,6 +160,9 @@ func (mi *ModuleInstance) newInstanceInfo() (*goja.Object, error) { return newInfoObj(rt, ti) } +//nolint:gochecknoglobals +var testInfoInitContextErr = common.NewInitContextError("getting test options in the init context is not supported") + // newTestInfo returns a goja.Object with property accessors to retrieve // information and control execution of the overall test run. func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) { @@ -176,8 +182,12 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) { } }, "options": func() interface{} { + vuState := mi.vu.State() + if vuState == nil { + common.Throw(rt, testInfoInitContextErr) + } if optionsObject == nil { - opts, err := optionsAsObject(rt, mi.vu.State().Options) + opts, err := optionsAsObject(rt, vuState.Options) if err != nil { common.Throw(rt, err) } @@ -190,12 +200,15 @@ func (mi *ModuleInstance) newTestInfo() (*goja.Object, error) { return newInfoObj(rt, ti) } +//nolint:gochecknoglobals +var vuInfoInitContextErr = common.NewInitContextError("getting VU information in the init context is not supported") + // newVUInfo returns a goja.Object with property accessors to retrieve // information about the currently executing VU. func (mi *ModuleInstance) newVUInfo() (*goja.Object, error) { vuState := mi.vu.State() if vuState == nil { - return nil, errors.New("getting VU information in the init context is not supported") + return nil, vuInfoInitContextErr } rt := mi.vu.Runtime() diff --git a/js/modules/k6/execution/execution_test.go b/js/modules/k6/execution/execution_test.go index 615d0f7f232a..35f522b4c239 100644 --- a/js/modules/k6/execution/execution_test.go +++ b/js/modules/k6/execution/execution_test.go @@ -444,6 +444,23 @@ func TestScenarioNoAvailableInInitContext(t *testing.T) { } } +func TestOptionsNoAvailableInInitContext(t *testing.T) { + t.Parallel() + + rt := goja.New() + m, ok := New().NewModuleInstance( + &modulestest.VU{ + RuntimeField: rt, + CtxField: context.Background(), + }, + ).(*ModuleInstance) + require.True(t, ok) + require.NoError(t, rt.Set("exec", m.Exports().Default)) + + _, err := rt.RunString("exec.test.options") + require.ErrorContains(t, err, "getting test options in the init context is not supported") +} + func TestVUDefaultDetails(t *testing.T) { t.Parallel()