Skip to content

Commit f5c4bba

Browse files
committed
chore: use requiremodule from pnpm PR
1 parent b8f4a80 commit f5c4bba

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

src/test/setup-webview.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,39 @@ chai.use(sinonChai);
55

66
// JSDom
77
import { 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

1842
const 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

3457
Object.assign(focusTrap, {
3558
...focusTrap,

0 commit comments

Comments
 (0)