Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit ed0de4f

Browse files
Ivan Mirićimiric
Ivan Mirić
authored andcommitted
Update testBrowser to new HTTP module initialization
1 parent e0cc1cc commit ed0de4f

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

tests/test_browser.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/stretchr/testify/require"
3636
k6common "go.k6.io/k6/js/common"
3737
k6http "go.k6.io/k6/js/modules/k6/http"
38+
k6modulestest "go.k6.io/k6/js/modulestest"
3839
k6lib "go.k6.io/k6/lib"
3940
k6metrics "go.k6.io/k6/lib/metrics"
4041
k6test "go.k6.io/k6/lib/testutils/httpmultibin"
@@ -104,23 +105,19 @@ func newTestBrowser(t testing.TB, opts ...interface{}) *testBrowser {
104105
BuiltinMetrics: k6metrics.RegisterBuiltinMetrics(k6metrics.NewRegistry()),
105106
}
106107

107-
rt := goja.New()
108+
rt, _ := getHTTPTestModuleInstance(t, ctx, state)
108109

109110
// enable the HTTP test server only when necessary
110111
var testServer *k6test.HTTPMultiBin
111112
if enableHTTPMultiBin {
112113
testServer = k6test.NewHTTPMultiBin(t)
113114
state.TLSConfig = testServer.TLSClientConfig
114115
state.Transport = testServer.HTTPTransport
115-
116-
err = rt.Set("http", k6common.Bind(rt, new(k6http.GlobalHTTP).NewModuleInstancePerVU(), &ctx))
117-
require.NoError(t, err)
118116
}
119117

120-
// configure the goja
118+
// configure goja
121119
ctx = k6lib.WithState(ctx, state)
122120
ctx = k6common.WithRuntime(ctx, rt)
123-
rt.SetFieldNameMapper(k6common.FieldNameMapper{})
124121

125122
// launch the browser
126123
bt := chromium.NewBrowserType(ctx).(*chromium.BrowserType)
@@ -275,3 +272,34 @@ func withFileServer() fileServerOption {
275272
// withContext is used to detect whether to use a custom context in the test
276273
// browser.
277274
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

Comments
 (0)