Skip to content

Commit fa2358d

Browse files
committed
feat: node:module stub mock
1 parent 4b72db4 commit fa2358d

4 files changed

Lines changed: 23 additions & 5 deletions

File tree

bundler/bundle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export const build = async (...files) => {
295295
'fs/promises': api('fs-promises.cjs'),
296296
http: api('http.cjs'),
297297
https: api('https.cjs'),
298+
module: api('module.cjs'),
298299
os: resolveRequire('os-browserify/browser.js'), // 'main' entry point is noop, we want browser entry
299300
path: resolveRequire('path-browserify'),
300301
querystring: resolveRequire('querystring-es3'),

bundler/modules/module.cjs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const ids = 'Module,SourceMap,builtinModules,findSourceMap,globalPaths,isBuiltin,runMain'.split(',')
2+
3+
const makeMethod = (key) => {
4+
// Not an arrow as there are classes that can be called with new
5+
return function () {
6+
throw new Error(`module.${key} unsupported in bundled mode`)
7+
}
8+
}
9+
10+
const createRequire = (filename) => (file) => {
11+
const clean = file.replace(/^node:/, '')
12+
if (globalThis.EXODUS_TEST_MOCK_BUILTINS?.has(clean)) return EXODUS_TEST_MOCK_BUILTINS.get(clean) // eslint-disable-line no-undef
13+
throw new Error(`module.createRequire is unsupported in bundled mode (origin: ${filename})`)
14+
}
15+
16+
module.exports = { ...Object.fromEntries(ids.map((key) => [key, makeMethod(key)])), createRequire }

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"bundler/modules/globals.cjs",
5959
"bundler/modules/jest-message-util.js",
6060
"bundler/modules/jest-util.js",
61+
"bundler/modules/module.cjs",
6162
"bundler/modules/node-buffer.cjs",
6263
"bundler/modules/text-encoding-utf.cjs",
6364
"bundler/modules/tty.cjs",

tests/jest/snapshot.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,16 @@ it('supports named snapshots', async () => {
256256

257257
// Additionally recheck named snapshot presence
258258

259-
let createRequire
259+
let snapshots
260260
try {
261-
;({ createRequire } = await import('node:module'))
261+
const { createRequire } = await import('node:module')
262+
const require = createRequire(import.meta.url)
263+
snapshots = require('./__snapshots__/snapshot.test.js.snap')
262264
} catch {
263-
// skip the rest of this test for environments without node:module
265+
// skip the rest of this test for environments without dynamic node:module
264266
return
265267
}
266268

267-
const require = createRequire(import.meta.url)
268-
const snapshots = require('./__snapshots__/snapshot.test.js.snap')
269269
if (Object.keys(snapshots).join(',') === 'default') return // Bun can't load .snap that way
270270
;[
271271
'supports named snapshots: public knowledge 1',

0 commit comments

Comments
 (0)