@@ -35,6 +35,7 @@ import (
35
35
"github.com/stretchr/testify/require"
36
36
k6common "go.k6.io/k6/js/common"
37
37
k6http "go.k6.io/k6/js/modules/k6/http"
38
+ k6modulestest "go.k6.io/k6/js/modulestest"
38
39
k6lib "go.k6.io/k6/lib"
39
40
k6metrics "go.k6.io/k6/lib/metrics"
40
41
k6test "go.k6.io/k6/lib/testutils/httpmultibin"
@@ -104,23 +105,19 @@ func newTestBrowser(t testing.TB, opts ...interface{}) *testBrowser {
104
105
BuiltinMetrics : k6metrics .RegisterBuiltinMetrics (k6metrics .NewRegistry ()),
105
106
}
106
107
107
- rt := goja . New ( )
108
+ rt , _ := getHTTPTestModuleInstance ( t , ctx , state )
108
109
109
110
// enable the HTTP test server only when necessary
110
111
var testServer * k6test.HTTPMultiBin
111
112
if enableHTTPMultiBin {
112
113
testServer = k6test .NewHTTPMultiBin (t )
113
114
state .TLSConfig = testServer .TLSClientConfig
114
115
state .Transport = testServer .HTTPTransport
115
-
116
- err = rt .Set ("http" , k6common .Bind (rt , new (k6http.GlobalHTTP ).NewModuleInstancePerVU (), & ctx ))
117
- require .NoError (t , err )
118
116
}
119
117
120
- // configure the goja
118
+ // configure goja
121
119
ctx = k6lib .WithState (ctx , state )
122
120
ctx = k6common .WithRuntime (ctx , rt )
123
- rt .SetFieldNameMapper (k6common.FieldNameMapper {})
124
121
125
122
// launch the browser
126
123
bt := chromium .NewBrowserType (ctx ).(* chromium.BrowserType )
@@ -275,3 +272,34 @@ func withFileServer() fileServerOption {
275
272
// withContext is used to detect whether to use a custom context in the test
276
273
// browser.
277
274
type withContext = context.Context
275
+
276
+ //nolint: golint, revive
277
+ // Copied from https://github.com/grafana/k6/blob/v0.36.0/js/modules/k6/http/http_test.go#L39
278
+ func getHTTPTestModuleInstance (
279
+ t testing.TB , ctx context.Context , state * k6lib.State ,
280
+ ) (* goja.Runtime , * k6http.ModuleInstance ) {
281
+ rt := goja .New ()
282
+ rt .SetFieldNameMapper (k6common.FieldNameMapper {})
283
+
284
+ if ctx == nil {
285
+ dummyCtx , cancel := context .WithCancel (context .Background ())
286
+ t .Cleanup (cancel )
287
+ ctx = dummyCtx
288
+ }
289
+
290
+ root := k6http .New ()
291
+ mockVU := & k6modulestest.VU {
292
+ RuntimeField : rt ,
293
+ InitEnvField : & k6common.InitEnvironment {
294
+ Registry : k6metrics .NewRegistry (),
295
+ },
296
+ CtxField : ctx ,
297
+ StateField : state ,
298
+ }
299
+ mi , ok := root .NewModuleInstance (mockVU ).(* k6http.ModuleInstance )
300
+ require .True (t , ok )
301
+
302
+ require .NoError (t , rt .Set ("http" , mi .Exports ().Default ))
303
+
304
+ return rt , mi
305
+ }
0 commit comments