Skip to content

Commit

Permalink
Make setTimeout and co. globally available in modulestest
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 26, 2024
1 parent 8184cec commit 294c316
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions js/modulestest/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"go.k6.io/k6/js/compiler"
"go.k6.io/k6/js/eventloop"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/js/modules/k6/timers"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/metrics"
Expand Down Expand Up @@ -64,6 +65,14 @@ func (r *Runtime) MoveToVUContext(state *lib.State) {
// SetupModuleSystem sets up the modules system for the Runtime.
// See [modules.NewModuleResolver] for the meaning of the parameters.
func (r *Runtime) SetupModuleSystem(goModules map[string]any, loader modules.FileLoader, c *compiler.Compiler) error {
if goModules == nil {
goModules = make(map[string]any)
}

if _, ok := goModules["k6/timers"]; !ok {
goModules["k6/timers"] = timers.New()
}

r.mr = modules.NewModuleResolver(goModules, loader, c)
return r.innerSetupModuleSystem()
}
Expand Down Expand Up @@ -105,5 +114,6 @@ func (r *Runtime) RunOnEventLoop(code string) (value goja.Value, err error) {
func (r *Runtime) innerSetupModuleSystem() error {
ms := modules.NewModuleSystem(r.mr, r.VU)
impl := modules.NewLegacyRequireImpl(r.VU, ms, url.URL{})
modules.ExportGloballyModule(r.VU.RuntimeField, ms, "k6/timers")
return r.VU.RuntimeField.Set("require", impl.Require)
}

0 comments on commit 294c316

Please sign in to comment.