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

Commit aaae86c

Browse files
author
Ivan Mirić
committed
Fix incompatibility with k6/js/modulestest
See grafana/k6#2602
1 parent 4dc8165 commit aaae86c

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

k6ext/k6test/vu.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package k6test
22

33
import (
4-
"context"
54
"testing"
65

76
"github.com/grafana/xk6-browser/k6ext"
@@ -10,16 +9,21 @@ import (
109
k6eventloop "go.k6.io/k6/js/eventloop"
1110
k6modulestest "go.k6.io/k6/js/modulestest"
1211
k6lib "go.k6.io/k6/lib"
12+
k6testutils "go.k6.io/k6/lib/testutils"
1313
k6metrics "go.k6.io/k6/metrics"
1414

1515
"github.com/dop251/goja"
1616
"github.com/oxtoacart/bpool"
17-
"github.com/sirupsen/logrus"
1817
"github.com/stretchr/testify/require"
1918
"gopkg.in/guregu/null.v3"
2019
)
2120

2221
// VU is a k6 VU instance.
22+
// TODO: Do we still need this VU wrapper?
23+
// ToGojaValue can be a helper function that takes a goja.Runtime (although it's
24+
// not much of a helper from calling ToValue(i) directly...), and we can access
25+
// EventLoop from modulestest.Runtime.EventLoop. I guess we still need the
26+
// RunLoop() override to call WaitOnRegistered()?
2327
type VU struct {
2428
*k6modulestest.VU
2529
Loop *k6eventloop.EventLoop
@@ -34,7 +38,7 @@ func (v *VU) RunLoop(fn func() error) error {
3438
return v.Loop.Start(fn)
3539
}
3640

37-
// NewVU returns a mock VU.
41+
// NewVU returns a mock k6 VU.
3842
func NewVU(tb testing.TB) *VU {
3943
tb.Helper()
4044

@@ -56,28 +60,18 @@ func NewVU(tb testing.TB) *VU {
5660
BatchPerHost: null.IntFrom(20),
5761
// HTTPDebug: null.StringFrom("full"),
5862
},
59-
Logger: logrus.StandardLogger(),
63+
Logger: k6testutils.NewLogger(tb),
6064
Group: root,
6165
BPool: bpool.NewBufferPool(1),
6266
Samples: samples,
6367
Tags: k6lib.NewTagMap(map[string]string{"group": root.Path}),
6468
BuiltinMetrics: k6metrics.RegisterBuiltinMetrics(k6metrics.NewRegistry()),
6569
}
66-
vu := &VU{
67-
VU: &k6modulestest.VU{
68-
RuntimeField: rt,
69-
InitEnvField: &k6common.InitEnvironment{
70-
Registry: k6metrics.NewRegistry(),
71-
},
72-
StateField: state,
73-
},
74-
}
75-
ctx := k6ext.WithVU(context.Background(), vu)
76-
vu.CtxField = ctx
7770

78-
loop := k6eventloop.New(vu)
79-
vu.RegisterCallbackField = loop.RegisterCallback
80-
vu.Loop = loop
71+
testRT := k6modulestest.NewRuntime(tb)
72+
ctx := k6ext.WithVU(testRT.VU.CtxField, testRT.VU)
73+
testRT.VU.CtxField = ctx
74+
testRT.MoveToVUContext(state)
8175

82-
return vu
76+
return &VU{VU: testRT.VU, Loop: testRT.EventLoop}
8377
}

tests/test_browser.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/grafana/xk6-browser/k6ext"
3434
"github.com/grafana/xk6-browser/k6ext/k6test"
3535

36+
k6common "go.k6.io/k6/js/common"
3637
k6http "go.k6.io/k6/js/modules/k6/http"
3738
k6httpmultibin "go.k6.io/k6/lib/testutils/httpmultibin"
3839
k6metrics "go.k6.io/k6/metrics"
@@ -123,11 +124,24 @@ func newTestBrowser(tb testing.TB, opts ...interface{}) *testBrowser {
123124
}
124125

125126
// launch the browser
127+
initEnv := &k6common.InitEnvironment{
128+
Logger: state.Logger,
129+
Registry: registry,
130+
}
131+
// NewBrowserType must be run from the init context, but since the recent
132+
// validation introduced in k6/js/modulestest, InitEnvField and StateField
133+
// must not be both set at the same time. So we nil StateField, create
134+
// the BrowserType, and revert it before running Launch().
135+
// See https://github.com/grafana/k6/pull/2602
136+
vu.InitEnvField = initEnv
137+
vu.StateField = nil
126138
v := chromium.NewBrowserType(vu)
127139
bt, ok := v.(*chromium.BrowserType)
128140
if !ok {
129141
panic(fmt.Errorf("testBrowser: unexpected browser type %T", v))
130142
}
143+
vu.InitEnvField = nil
144+
vu.StateField = state
131145
b := bt.Launch(rt.ToValue(launchOpts))
132146
tb.Cleanup(func() {
133147
select {

0 commit comments

Comments
 (0)