dosubot[bot] commented on issue #30297:
URL: https://github.com/apache/superset/issues/30297#issuecomment-2353654061

   <!-- Answer -->
   I found a similar unsolved discussion that might be relevant to your issue: 
[Building Custom Viz Plugins in 
Superset](https://github.com/apache/superset/discussions/29641) 
<sup>[[1]](https://github.com/apache/superset/discussions/29641)</sup>. 
Additionally, there was a closed issue related to module resolution: [Cannot 
find module 
'@airbnb/config-babel'](https://github.com/apache/superset/issues/24640) 
<sup>[[2]](https://github.com/apache/superset/issues/24640)</sup>.
   
   To resolve the "ReferenceError: window is not defined" error when running 
tests for a custom visualization plugin in Superset, you need to ensure that 
the global `window` object is properly mocked in your test environment. The 
provided `shim.tsx` file from the Superset repository already includes the 
necessary setup to mock the `window` object.
   
   Here is the relevant part of the `shim.tsx` file that sets up the global 
`window` object:
   
   ```tsx
   const g = global as any;
   g.window ??= Object.create(window);
   g.window.location ??= { href: 'about:blank' };
   g.window.performance ??= { now: () => new Date().getTime() };
   g.window.Worker ??= Worker;
   g.window.IntersectionObserver ??= IntersectionObserver;
   g.window.ResizeObserver ??= ResizeObserver;
   g.window.featureFlags ??= {};
   g.URL.createObjectURL ??= () => '';
   g.caches = new CacheStorage();
   
   Object.defineProperty(window, 'matchMedia', {
     writable: true,
     value: jest.fn().mockImplementation(query => ({
       matches: false,
       media: query,
       onchange: null,
       addListener: jest.fn(), // Deprecated
       removeListener: jest.fn(), // Deprecated
       addEventListener: jest.fn(),
       removeEventListener: jest.fn(),
       dispatchEvent: jest.fn(),
     })),
   });
   
   g.$ = jQuery(g.window);
   ```
   
   Ensure that this `shim.tsx` file is imported and executed in your test 
setup. This will mock the `window` object and prevent the "ReferenceError: 
window is not defined" error.
   
   If you haven't already, you can include this setup in your Jest 
configuration by adding the following to your Jest setup file (e.g., 
`setupTests.ts`):
   
   ```tsx
   import './path/to/shim.tsx';
   ```
   
   This will ensure that the `window` object and other necessary mocks are 
available in your test environment 
<sup>[[3]](https://github.com/apache/superset/blob/master/superset-frontend/spec/helpers/shim.tsx)</sup>.
   
   
   <!-- Dosu Comment Footer -->
   *To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to