If a component uses ResizeObserver, Jest fails with:
ReferenceError: ResizeObserver is not defined
ResizeObserver is a browser API. Jest runs your tests in Node.js, and jsdom, the fake DOM Jest uses, doesn't implement ResizeObserver either. jsdom has no layout engine, so there are no sizes for it to observe. The fix is to mock it in your Jest setup file.
The component throwing the error is often not yours. Chart and UI libraries call ResizeObserver internally, for example Recharts' ResponsiveContainer and several Radix UI primitives, so the stack trace may point into node_modules or react-dom.
How to mock ResizeObserver
Add this to your Jest setup file (for example jest-setup.js, registered under setupFilesAfterEnv in your Jest config):
// jest-setup.js
global.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
// jest.config.js
module.exports = {
testEnvironment: "jsdom",
setupFilesAfterEnv: ["<rootDir>/jest-setup.js"],
};
This defines an empty ResizeObserver class globally with the three methods your code calls: observe, unobserve and disconnect. They do nothing, but that's enough for code that only needs ResizeObserver to exist.
Same fix in Vitest
Vitest with environment: "jsdom" throws the same error. Put the mock in a file listed under test.setupFiles, and use globalThis, which works in both runners:
// vitest.setup.ts
globalThis.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
};
// vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
environment: "jsdom",
setupFiles: ["./vitest.setup.ts"],
},
});
Firing the callback in a test
The empty mock never calls the callback, so a component that changes layout on resize renders only its initial state. If you need to test the resized state, keep a reference to each observer and fire the callback yourself:
// jest-setup.js
class ResizeObserverMock {
static instances = [];
constructor(callback) {
this.callback = callback;
this.targets = new Set();
ResizeObserverMock.instances.push(this);
}
observe(target) {
this.targets.add(target);
}
unobserve(target) {
this.targets.delete(target);
}
disconnect() {
this.targets.clear();
}
resize(width, height) {
const entries = [...this.targets].map((target) => ({
target,
contentRect: { width, height },
}));
this.callback(entries, this);
}
}
global.ResizeObserver = ResizeObserverMock;
global.ResizeObserverMock = ResizeObserverMock;
beforeEach(() => {
ResizeObserverMock.instances = [];
});
Then trigger a resize inside act so React applies the state update:
import { act, render, screen } from "@testing-library/react";
it("switches to the compact layout below 400px", () => {
render(<Toolbar />);
act(() => {
ResizeObserverMock.instances[0].resize(320, 40);
});
expect(screen.getByRole("button", { name: "More" })).toBeInTheDocument();
});
The entries only include target and contentRect. If your code reads borderBoxSize or contentBoxSize, add those to the objects in resize().
Why not install a polyfill?
Packages like resize-observer-polyfill make the error go away, but they measure elements with getBoundingClientRect, and jsdom returns 0 for every size. The callback either never fires or reports 0×0, which is rarely the state you want to test. A mock you control is shorter and lets you pick the size.