Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot redefine property: ResizeObserver (v1.4.0) #37

Closed
DesselBane opened this issue Jul 21, 2022 · 2 comments
Closed

TypeError: Cannot redefine property: ResizeObserver (v1.4.0) #37

DesselBane opened this issue Jul 21, 2022 · 2 comments
Labels
question Further information is requested

Comments

@DesselBane
Copy link

Hi

with version 1.4.0 this error starts to appear.

Node version 14.20.0
Jest version 27.5.1
jsdom => jest-environment-jsdom@27.5.1

 FAIL  src/components/object-message/object-message.unit.js
  ● Test suite failed to run

    TypeError: Cannot redefine property: ResizeObserver
        at Function.defineProperty (<anonymous>)

       6 | import { click, getByText, mountToDom } from '@testutils'
       7 |
    >  8 | const resizeObserverMock = mockResizeObserver()
         |                            ^
       9 |
      10 | jest.mock('lodash/debounce', () => {
      11 |   return jest.fn((fn) => fn)

      at mockResizeObserver (node_modules/jsdom-testing-mocks/src/mocks/resize-observer.ts:101:10)
      at Object.<anonymous> (src/components/object-message/object-message.unit.js:8:28)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:404:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:320:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:173:3)
@trurl-master
Copy link
Owner

Hi there! Thank you for the issue.

A wild guess here. It seems that in your case the ResizeObserver is already defined. A polyfill maybe? Another mock? The cannot redefine property error would arise if the polyfill was applied, in such manner, for example:

  Object.defineProperty(window, 'ResizeObserver', {
    writable: true,
    configurable: false,
    value: PolyfilledResizeObserverClass,
  });

Note the configurable: false. In that case my code wouldn't be able to redefine it. Can you check if it's the case, please? I changed how I mock the ResizeObserver class from basically:

window.ResizeObserver = MockedResizeObserver

to

  Object.defineProperty(window, 'ResizeObserver', {
    writable: true,
    configurable: true,
    value: MockedResizeObserver,
  });

Because it is the recommended way, but maybe isn't the best.

Maybe you can disable the polyfill for tests somehow?

@trurl-master trurl-master added the question Further information is requested label Jan 24, 2023
@trurl-master
Copy link
Owner

Will close this for now. Feel free to reopen if needed

@trurl-master trurl-master closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants