You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
describe('useEscapeKey hook unit tests', () => {
let callback = vi.fn()
afterEach(() => {
callback.mockReset()
})
it('should add and remove the keydown event listener on mount and unmount', () => {
const { unmount } = renderHook(() => useEscapeKey(callback))
// Check if the event listener is attached
const addEventListenerSpy = vi.spyOn(document, 'addEventListener')
expect(addEventListenerSpy).toBeCalledTimes(1)
// // Unmount the hook and check if the listener is removed
const removeEventListenerSpy = vi.spyOn(document, 'removeEventListener')
unmount()
expect(removeEventListenerSpy).toHaveBeenCalledWith('keydown', expect.any(Function))
addEventListenerSpy.mockRestore()
removeEventListenerSpy.mockRestore()
})
})
The error I get:
⎯⎯⎯⎯⎯⎯ Unhandled Errors ⎯⎯⎯⎯⎯⎯
Vitest caught 1 unhandled error during the test run.
This might cause false positive tests. Resolve unhandled errors to make sure your tests are not affected.
⎯⎯⎯⎯⎯⎯ Unhandled Error ⎯⎯⎯⎯⎯⎯⎯
TypeError: s.getBrowserSourceMapModuleById is not a function
❯ Object.getSourceMap ../../../.vscode/extensions/vitest.explorer-0.10.7/dist/worker.js:3:944
❯ ../../../.vscode/extensions/vitest.explorer-0.10.7/dist/worker.js:1:18634
❯ Mt ../../../.vscode/extensions/vitest.explorer-0.10.7/dist/worker.js:1:18580
❯ Oe ../../../.vscode/extensions/vitest.explorer-0.10.7/dist/worker.js:3:130
❯ ../../../.vscode/extensions/vitest.explorer-0.10.7/dist/worker.js:3:920
❯ ../../../.vscode/extensions/vitest.explorer-0.10.7/dist/worker.js:3:875
❯ V.onTaskUpdate ../../../.vscode/extensions/vitest.explorer-0.10.7/dist/worker.js:3:809
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
I found that error thrown by next row: expect(addEventListenerSpy).toBeCalledTimes(1)
Looks like v2.0.0 has an issue with document spying❓
Regarding your reproduction. It doesn't have jsdom installed, so every test fails. After installing jsdom, some tests still fail:
This test fails because you need to spy on document before you attack the listener, how else would it intercept it?
const{ unmount }=renderHook(()=>useEscapeKey(callback));// Check if the event listener is attachedconstaddEventListenerSpy=vi.spyOn(document,'addEventListener');expect(addEventListenerSpy).toBeCalledTimes(1);
After moving it before the render call, the new error is that addEventListenerSpy is called more than once. This is not an issue with Vitest, but with how react handles useEffect. I cannot give you any advice here, I suggest asking in Discord or open a community discussion.
I think it would be beneficial to add a note to the v2.0.0 breaking change description mentioning that the new version is incompatible with the older Vitest VSCode extension. This would have saved me a lot of time trying to resolve the unit test errors I encountered after updating to version 2.0.0.
Describe the bug
I have next very simple react hook that catches escape key down event:
import { DependencyList, useEffect } from 'react'
I added unit tests like:
The error I get:
I found that error thrown by next row:
expect(addEventListenerSpy).toBeCalledTimes(1)
Looks like v2.0.0 has an issue with document spying❓
Reproduction
https://stackblitz.com/edit/vitest-dev-vitest-9wx4pc?file=test%2FuseEscapeKey.spec.ts
System Info
Used Package Manager
pnpm
Validations
The text was updated successfully, but these errors were encountered: