grafana/k6 · error

invalid number of arguments to ws.connect

Error message

invalid number of arguments to ws.connect

What it means

Thrown by ws.connect in the legacy k6/ws module when the arguments after the URL are neither exactly one (the setup function) nor exactly two (params object plus setup function). parseConnectArgs (internal/js/modules/k6/ws/ws.go:635) switches on len(args) and rejects every other arity with 'invalid number of arguments to ws.connect'.

Solutions

  1. Use the canonical form: ws.connect('wss://example.com', { tags: { ... } }, (socket) => { ... })
  2. For no params, drop the middle object: ws.connect('wss://example.com', (socket) => { ... })
  3. If you need to pass more configuration, merge it into the single params object rather than adding arguments

Example fix

// before
ws.connect('wss://example.com'); // no setup function

// after
ws.connect('wss://example.com', (socket) => {
  socket.on('open', () => console.log('connected'));
});
Defensive patterns

Strategy: validation

Validate before calling

function connectWs(url, params, setup) {
  const hasParams = typeof params === 'object' && params !== null;
  const fn = hasParams ? setup : params;
  const p = hasParams ? params : {};
  if (typeof fn !== 'function') throw new TypeError('ws.connect requires a setup function as its last argument');
  return hasParams ? ws.connect(url, p, fn) : ws.connect(url, fn);
}

Prevention

When it happens

Trigger: ws.connect('wss://example.com') with no callback at all, or ws.connect('wss://example.com', { tags: {...} }, function(socket) {...}, extra) with three trailing arguments. Note the URL itself is a separate leading parameter, so the total JS arity must be 2 or 3.

Common situations: Forgetting the setup callback during quick prototyping; appending extra arguments (e.g. a second config object) when porting code from another WebSocket API; a refactor that split params into two objects.

Related errors


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/7dd77d6b7d6f0e64. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/ws/ws.go:635

	for k, vs := range httpResponse.Header {
		wsResponse.Headers[k] = strings.Join(vs, ", ")
	}

	return &wsResponse, nil
}

func parseConnectArgs(state *lib.State, rt *sobek.Runtime, args ...sobek.Value) (*wsConnectArgs, error) {
	// The params argument is optional
	var callableV, paramsV sobek.Value
	switch len(args) {
	case 2:
		paramsV = args[0]
		callableV = args[1]
	case 1:
		paramsV = sobek.Undefined()
		callableV = args[0]
	default:
		return nil, errors.New("invalid number of arguments to ws.connect")
	}
	// Get the callable (required)
	setupFn, isFunc := sobek.AssertFunction(callableV)
	if !isFunc {
		return nil, errors.New("last argument to ws.connect must be a function")
	}

	headers := make(http.Header)
	headers.Set("User-Agent", state.Options.UserAgent.String)
	tagsAndMeta := state.Tags.GetCurrentValues()
	parsedArgs := &wsConnectArgs{
		setupFn:     setupFn,
		headers:     headers,
		cookieJar:   state.CookieJar,
		tagsAndMeta: &tagsAndMeta,
	}

	if common.IsNullish(paramsV) {

View on GitHub (pinned to 01ffac6f24)