When running frontend tests, you may get this error:
TypeError: window.scrollTo is not a function
This is normal in Jest + jsdom, because some browser APIs are not implemented by default.
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.