{"record":{"id":"63605f7e7c647d2d","repo":"vitest-dev/vitest","slug":"commands-are-only-available-for-browser-tests","errorCode":null,"errorMessage":"Commands are only available for browser tests.","messagePattern":"Commands are only available for browser tests\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/src/node/rpc.ts","lineNumber":329,"sourceCode":"          }\n          return result?.map\n        },\n        cancelCurrentRun(reason) {\n          vitest.cancelCurrentRun(reason)\n        },\n        async resolveId(id, importer) {\n          return mockResolver.resolveId(id, importer)\n        },\n        debug(...args) {\n          vitest.logger.console.debug(...args)\n        },\n        getCountOfFailedTests() {\n          return vitest.state.getCountOfFailedTests()\n        },\n        async wdioSwitchContext(direction) {\n          const provider = project.browser!.provider\n          if (!provider) {\n            throw new Error('Commands are only available for browser tests.')\n          }\n          if (provider.name !== 'webdriverio') {\n            throw new Error('Switch context is only available for WebDriverIO provider.')\n          }\n          if (direction === 'iframe') {\n            await (provider as any).switchToTestFrame()\n          }\n          else {\n            await (provider as any).switchToMainFrame()\n          }\n        },\n        async triggerCommand(sessionId, command, testPath, payload) {\n          debug?.('[%s] Triggering command \"%s\"', sessionId, command)\n          const provider = project.browser!.provider\n          if (!provider) {\n            throw new Error('Commands are only available for browser tests.')\n          }\n          const context = Object.assign(","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/browser/src/node/rpc.ts#L311-L347","documentation":"The wdioSwitchContext RPC reads project.browser!.provider and requires it to be defined before checking the provider name. If the project is not a browser project or the provider was not initialized, the non-null assertion on browser is followed by an undefined provider, and the code throws this generic 'Commands are only available for browser tests' message rather than crashing on a deeper call.","triggerScenarios":"Calling the wdioSwitchContext RPC from a project that has no browser instance, or whose provider has not yet been initialized; calling it during teardown after the provider was cleared.","commonSituations":"Mixing node and browser projects and routing a context-switch command to a node project; invoking the RPC before the browser provider initialized; a stale connection sending commands after the run ended.","solutions":["Ensure the command is invoked against a project that has a browser instance with an initialized provider.","Guard the call site by checking project.browser?.provider is defined first.","Avoid issuing context-switch commands after the test run finishes."],"exampleFix":"// before\nawait rpc.wdioSwitchContext('iframe') // project has no provider\n// after\nif (project.browser?.provider) await rpc.wdioSwitchContext('iframe')","handlingStrategy":"validation","validationCode":"function browserProviderReady(browser?: { provider?: unknown }): boolean {\n  return !!browser?.provider\n}","typeGuard":"function isBrowserProject(project: unknown): project is { browser: { provider: { name: string } } } {\n  return !!project && typeof project === 'object' && !!(project as any).browser?.provider\n}","tryCatchPattern":null,"preventionTips":["Route context-switch commands only to browser projects with an initialized provider.","Guard call sites with project.browser?.provider checks.","Avoid issuing commands after the test run has torn down."],"tags":["browser-command","webdriverio","provider","context-switch","browser"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}