When running frontend tests, you may get one of these errors:
Error: Not implemented: window.scrollTo
TypeError: window.scrollTo is not a function
The first is jsdom saying the method is a stub. The second shows up when window.scrollTo is missing entirely. Both are normal in Jest + jsdom, because jsdom does not implement some browser APIs.
Fast fix in Jest setup
Add this in your jest.setup.ts (or setup file):
Object.defineProperty(window, 'scrollTo', {
value: jest.fn(),
writable: true,
});
Now tests calling window.scrollTo will no longer crash.
Alternative mock style
You can also assign directly:
window.scrollTo = jest.fn();
Either works, but defineProperty is safer when property descriptors are strict.
Verify in a test
it('calls scrollTo on open', () => {
render(<MyModal />);
fireEvent.click(screen.getByRole('button', { name: /open/i }));
expect(window.scrollTo).toHaveBeenCalled();
});
This confirms the mock is active and your behavior is testable.
Register setup file in Jest config
Make sure Jest uses your setup file:
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts']
If this is missing, your mock will not load.