grafana/k6 · error

last argument to ws.connect must be a function

Error message

last argument to ws.connect must be a function

What it means

Thrown by ws.connect in the legacy k6/ws module when the last argument — the required setup function — is not callable. parseConnectArgs (internal/js/modules/k6/ws/ws.go:640) runs sobek.AssertFunction on it and rejects strings, objects, and undefined with 'last argument to ws.connect must be a function'.

Solutions

  1. Always end the call with the setup function: ws.connect('wss://example.com', params, (socket) => { ... })
  2. If the callback lives in a variable, verify it is defined and a function before the call
  3. Check argument order when porting examples — URL first, optional params in the middle, function last

Example fix

// before
ws.connect('wss://example.com', { tags: { name: 'ws' } }); // no callback

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

Strategy: type-guard

Validate before calling

if (typeof setupFn !== 'function') {
  throw new TypeError(`ws.connect setup must be a function, got ${typeof setupFn}`);
}
ws.connect('wss://example.com', { tags: { name: 'ws' } }, setupFn);

Type guard

function isFunction(v) {
  return typeof v === 'function';
}

Prevention

When it happens

Trigger: ws.connect('wss://example.com', { headers: { ... } }) — params supplied but the callback forgotten, so the params object is the last argument; or ws.connect('wss://example.com', 'setup') passing a function name or string instead of the function itself.

Common situations: Refactoring that extracts the inline callback but forgets to pass it; passing a function reference that is undefined because of a typo or missing import; copying an example that uses a different argument order.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

}

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) {
		return parsedArgs, nil
	}

	// Parse the optional second argument (params)
	params := paramsV.ToObject(rt)

View on GitHub (pinned to 01ffac6f24)