Because TanStack Hotkeys uses page-level singletons for the hotkey manager and held-key tracker, two practices keep component and hook tests deterministic across operating systems and test files: resetting those singletons after each test, and pinning platform.
Unmounting a root cleans up its useHotkey registrations, but registrations added via manager.register, scopes from activateScope, and registrations left behind by failed tests stay in the singleton instances.
Reset HotkeyManager in afterEach (and KeyStateTracker if testing held keys), and unmount the root the test created:
// Editor.tsrx
import { useHotkey } from '@tanstack/octane-hotkeys'
export function Editor({ onSave }: { onSave: () => void }) @{
useHotkey('Mod+S', onSave, { platform: 'mac' })
<textarea />
}// Editor.test.ts
import { afterEach, expect, it, vi } from 'vitest'
import { createRoot, flushSync } from 'octane'
import { HotkeyManager, KeyStateTracker } from '@tanstack/octane-hotkeys'
import { Editor } from './Editor.tsrx'
let root: ReturnType<typeof createRoot> | undefined
let container: HTMLDivElement | undefined
afterEach(() => {
root?.unmount()
root = undefined
container?.remove()
container = undefined
HotkeyManager.resetInstance()
KeyStateTracker.resetInstance()
})
it('saves with Mod+S', () => {
const save = vi.fn()
container = document.body.appendChild(document.createElement('div'))
root = createRoot(container)
flushSync(() => root!.render(Editor, { onSave: save }))
document.dispatchEvent(
new KeyboardEvent('keydown', { key: 's', metaKey: true, bubbles: true }),
)
expect(save).toHaveBeenCalledOnce()
})resetInstance() removes all listeners and registrations so the next call creates a fresh manager.
Simulated DOMs don't always reflect the development or CI operating system. For instance, happy-dom on macOS detects 'windows', which changes what Mod resolves to. Pass platform: 'mac' (or 'windows' or 'linux') in the hook options, or set it once for the entire test tree through HotkeysProvider:
<HotkeysProvider defaultOptions={{ platform: 'mac' }}>
<Editor />
</HotkeysProvider>const event = new KeyboardEvent('keydown', {
key: 's',
code: 'KeyS',
metaKey: true,
bubbles: true,
cancelable: true,
})
document.dispatchEvent(event)
expect(event.defaultPrevented).toBe(true)Dispatch each step in order, and use fake timers to verify the sequence timeout (1000 ms by default):
vi.useFakeTimers()
try {
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'g', bubbles: true }))
vi.advanceTimersByTime(500)
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'h', bubbles: true }))
expect(goHome).toHaveBeenCalledOnce()
} finally {
vi.useRealTimers()
}For hotkeys scoped to an element target, or to verify ignoreInputs inside an <input> or <textarea>, focus the element first and dispatch the event directly on it with bubbles: true:
const input = container.querySelector('input')!
input.focus()
input.dispatchEvent(new KeyboardEvent('keydown', { key: 'k', bubbles: true }))
expect(shortcutCallback).not.toHaveBeenCalled()