11import type { Options as TestingLibraryOptions , UserEvent as TestingLibraryUserEvent } from '@testing-library/user-event'
2- import type { BrowserRPC } from '@vitest/browser/client'
32import type { RunnerTask } from 'vitest'
43import type {
54 BrowserPage ,
@@ -19,15 +18,11 @@ import { convertElementToCssSelector, ensureAwaited, getBrowserState, getWorkerS
1918const state = ( ) => getWorkerState ( )
2019// @ts -expect-error not typed global
2120const provider = __vitest_browser_runner__ . provider
22- function filepath ( ) {
23- return getWorkerState ( ) . filepath || getWorkerState ( ) . current ?. file ?. filepath || undefined
24- }
25- const rpc = ( ) => getWorkerState ( ) . rpc as any as BrowserRPC
2621const sessionId = getBrowserState ( ) . sessionId
2722const channel = new BroadcastChannel ( `vitest:${ sessionId } ` )
2823
2924function triggerCommand < T > ( command : string , ...args : any [ ] ) {
30- return rpc ( ) . triggerCommand < T > ( sessionId , command , filepath ( ) , args )
25+ return getBrowserState ( ) . commands . triggerCommand < T > ( command , args )
3126}
3227
3328export function createUserEvent ( __tl_user_event_base__ ?: TestingLibraryUserEvent , options ?: TestingLibraryOptions ) : UserEvent {
@@ -52,6 +47,10 @@ export function createUserEvent(__tl_user_event_base__?: TestingLibraryUserEvent
5247 return createUserEvent ( )
5348 } ,
5449 async cleanup ( ) {
50+ // avoid cleanup rpc call if there is nothing to cleanup
51+ if ( ! keyboard . unreleased . length ) {
52+ return
53+ }
5554 return ensureAwaited ( async ( ) => {
5655 await triggerCommand ( '__vitest_cleanup' , keyboard )
5756 keyboard . unreleased = [ ]
@@ -106,9 +105,7 @@ export function createUserEvent(__tl_user_event_base__?: TestingLibraryUserEvent
106105 } )
107106 } ,
108107 tab ( options : UserEventTabOptions = { } ) {
109- return ensureAwaited ( ( ) => {
110- return triggerCommand ( '__vitest_tab' , options )
111- } )
108+ return ensureAwaited ( ( ) => triggerCommand ( '__vitest_tab' , options ) )
112109 } ,
113110 async keyboard ( text : string ) {
114111 return ensureAwaited ( async ( ) => {
0 commit comments