{"record":{"id":"7dd77d6b7d6f0e64","repo":"grafana/k6","slug":"invalid-number-of-arguments-to-ws-connect","errorCode":null,"errorMessage":"invalid number of arguments to ws.connect","messagePattern":"invalid number of arguments to ws\\.connect","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/ws/ws.go","lineNumber":635,"sourceCode":"\tfor k, vs := range httpResponse.Header {\n\t\twsResponse.Headers[k] = strings.Join(vs, \", \")\n\t}\n\n\treturn &wsResponse, nil\n}\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) {","sourceCodeStart":617,"sourceCodeEnd":653,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/ws/ws.go#L617-L653","documentation":"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'.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the canonical form: ws.connect('wss://example.com', { tags: { ... } }, (socket) => { ... })","For no params, drop the middle object: ws.connect('wss://example.com', (socket) => { ... })","If you need to pass more configuration, merge it into the single params object rather than adding arguments"],"exampleFix":"// before\nws.connect('wss://example.com'); // no setup function\n\n// after\nws.connect('wss://example.com', (socket) => {\n  socket.on('open', () => console.log('connected'));\n});","handlingStrategy":"validation","validationCode":"function connectWs(url, params, setup) {\n  const hasParams = typeof params === 'object' && params !== null;\n  const fn = hasParams ? setup : params;\n  const p = hasParams ? params : {};\n  if (typeof fn !== 'function') throw new TypeError('ws.connect requires a setup function as its last argument');\n  return hasParams ? ws.connect(url, p, fn) : ws.connect(url, fn);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Memorize the two valid shapes: ws.connect(url, fn) and ws.connect(url, params, fn)","Merge extra configuration into the single params object instead of adding arguments","Lint for zero-argument calls to connect during code review"],"tags":["websocket","k6-ws","connect","arguments","arity"],"backgroundTag":"wrong-number-of-arguments","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"}