trurl-master / jsdom-testing-mocks

A set of tools for emulating browser behavior in jsdom environment
MIT License
116 stars 7 forks source link

LocalResizeObserver is not a constructor error in v1.10+ #53

Closed viveleroi closed 1 year ago

viveleroi commented 1 year ago

When updating this library I noticed that some of our chart tests (charts use visx and the resize component). They pass fine in 1.9.0 but fail in 1.10+.

TypeError: LocalResizeObserver is not a constructor
 ❯ node_modules/@visx/responsive/lib/components/ParentSize.js:61:20
 ❯ commitHookEffectListMount node_modules/react-dom/cjs/react-dom.development.js:23150:26
 ❯ commitPassiveMountOnFiber node_modules/react-dom/cjs/react-dom.development.js:24931:11
 ❯ commitPassiveMountEffects_complete node_modules/react-dom/cjs/react-dom.development.js:24891:9
 ❯ commitPassiveMountEffects_begin node_modules/react-dom/cjs/react-dom.development.js:24878:7
 ❯ commitPassiveMountEffects node_modules/react-dom/cjs/react-dom.development.js:24866:3
 ❯ flushPassiveEffectsImpl node_modules/react-dom/cjs/react-dom.development.js:27039:3
 ❯ flushPassiveEffects node_modules/react-dom/cjs/react-dom.development.js:26984:14
 ❯ node_modules/react-dom/cjs/react-dom.development.js:26769:9
 ❯ flushActQueue node_modules/react/cjs/react.development.js:2667:24

I have the library configs setup like this:

import { act, cleanup } from '@testing-library/react'
import { afterEach, beforeAll, vi } from 'vitest'
import { configMocks } from 'jsdom-testing-mocks'
import '@testing-library/jest-dom/vitest'

configMocks({ act })
trurl-master commented 1 year ago

Hey @viveleroi ! Thanks for the issue.

Here's my guess: in 1.10.0 I fixed a bug where I accidentally initialized ResizeObserver mock in the module itself, that means that just importing the package was enough to mock the ResizeObserver. If this was the behaviour you relied on in your code, I'm afraid you will need to adapt a little bit, try explicitly calling mockResizeObserver where you need it and tell me how it goes! Sorry for the confusion!

viveleroi commented 1 year ago

Yup that solves it, thanks.