{"record":{"id":"08466ce78ccdcc07","repo":"grafana/k6","slug":"last-argument-to-ws-connect-must-be-a-function","errorCode":null,"errorMessage":"last argument to ws.connect must be a function","messagePattern":"last argument to ws\\.connect must be a function","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/ws/ws.go","lineNumber":640,"sourceCode":"}\n\nfunc parseConnectArgs(state *lib.State, rt *sobek.Runtime, args ...sobek.Value) (*wsConnectArgs, error) {\n\t// The params argument is optional\n\tvar callableV, paramsV sobek.Value\n\tswitch len(args) {\n\tcase 2:\n\t\tparamsV = args[0]\n\t\tcallableV = args[1]\n\tcase 1:\n\t\tparamsV = sobek.Undefined()\n\t\tcallableV = args[0]\n\tdefault:\n\t\treturn nil, errors.New(\"invalid number of arguments to ws.connect\")\n\t}\n\t// Get the callable (required)\n\tsetupFn, isFunc := sobek.AssertFunction(callableV)\n\tif !isFunc {\n\t\treturn nil, errors.New(\"last argument to ws.connect must be a function\")\n\t}\n\n\theaders := make(http.Header)\n\theaders.Set(\"User-Agent\", state.Options.UserAgent.String)\n\ttagsAndMeta := state.Tags.GetCurrentValues()\n\tparsedArgs := &wsConnectArgs{\n\t\tsetupFn:     setupFn,\n\t\theaders:     headers,\n\t\tcookieJar:   state.CookieJar,\n\t\ttagsAndMeta: &tagsAndMeta,\n\t}\n\n\tif common.IsNullish(paramsV) {\n\t\treturn parsedArgs, nil\n\t}\n\n\t// Parse the optional second argument (params)\n\tparams := paramsV.ToObject(rt)","sourceCodeStart":622,"sourceCodeEnd":658,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/ws/ws.go#L622-L658","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Always end the call with the setup function: ws.connect('wss://example.com', params, (socket) => { ... })","If the callback lives in a variable, verify it is defined and a function before the call","Check argument order when porting examples — URL first, optional params in the middle, function last"],"exampleFix":"// before\nws.connect('wss://example.com', { tags: { name: 'ws' } }); // no callback\n\n// after\nws.connect('wss://example.com', { tags: { name: 'ws' } }, (socket) => {\n  socket.on('open', () => console.log('connected'));\n});","handlingStrategy":"type-guard","validationCode":"if (typeof setupFn !== 'function') {\n  throw new TypeError(`ws.connect setup must be a function, got ${typeof setupFn}`);\n}\nws.connect('wss://example.com', { tags: { name: 'ws' } }, setupFn);","typeGuard":"function isFunction(v) {\n  return typeof v === 'function';\n}","tryCatchPattern":null,"preventionTips":["Always pass the callback inline or as a verified function reference","After extracting callbacks into named functions, assert they are defined before use","Keep the order in mind: URL first, optional params middle, function last"],"tags":["websocket","k6-ws","connect","callback","type-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}