@@ -5,15 +5,39 @@ chai.use(sinonChai);
55
66// JSDom
77import { JSDOM , VirtualConsole } from 'jsdom' ;
8+ import { createRequire } from 'module' ;
9+
10+ // Create a require function that works in both ESM and CommonJS contexts
11+ const requireModule = ( ( ) => {
12+ if ( typeof require !== 'undefined' && typeof require . cache === 'object' ) {
13+ // We're in CommonJS context
14+ return require ;
15+ }
16+
17+ // We're in ESM context, create require from import.meta.url
18+ // @ts -ignore
19+ if ( typeof import . meta !== 'undefined' && import . meta. url ) {
20+ // @ts -ignore
21+ return createRequire ( import . meta. url ) ;
22+ }
23+
24+ // Fallback: try to create require from current file location
25+ if ( typeof __filename !== 'undefined' ) {
26+ return createRequire ( __filename ) ;
27+ }
28+
29+ throw new Error (
30+ 'Unable to create require function - neither ESM nor CommonJS context detected' ,
31+ ) ;
32+ } ) ( ) ;
833
934/**
1035 * NB: focus-trap and tabbable require special overrides to work in jsdom environments as per
1136 * documentation
1237 *
1338 * @see {@link https://github.com/focus-trap/tabbable?tab=readme-ov-file#testing-in-jsdom }
1439 */
15- // eslint-disable-next-line @typescript-eslint/no-var-requires
16- const tabbable = require ( 'tabbable' ) ;
40+ const tabbable = requireModule ( 'tabbable' ) ;
1741
1842const origTabbable = { ...tabbable } ;
1943
@@ -28,8 +52,7 @@ Object.assign(tabbable, {
2852 origTabbable . isTabbable ( node , { ...options , displayCheck : 'none' } ) ,
2953} ) ;
3054
31- // eslint-disable-next-line @typescript-eslint/no-var-requires
32- const focusTrap = require ( 'focus-trap' ) ;
55+ const focusTrap = requireModule ( 'focus-trap' ) ;
3356
3457Object . assign ( focusTrap , {
3558 ...focusTrap ,
0 commit comments