2026-04-10 15:52:59 +09:00
|
|
|
import { createRequire } from "node:module"
|
2026-05-30 13:44:54 +09:00
|
|
|
import { fileURLToPath } from "node:url"
|
|
|
|
|
import { defaultGetCallerStack, isModuleEvaluationStack, resolveCallerUrlFromStack } from "./module-mock-stack"
|
2026-04-10 15:52:59 +09:00
|
|
|
|
|
|
|
|
type MockModuleFactory = () => Record<string, unknown>
|
|
|
|
|
|
|
|
|
|
type MockApi = {
|
|
|
|
|
module: (specifier: string, factory: MockModuleFactory) => unknown
|
|
|
|
|
restore: () => unknown
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ModuleLoadResult =
|
|
|
|
|
| { ok: true; value: unknown }
|
|
|
|
|
| { ok: false; error: Error }
|
|
|
|
|
|
|
|
|
|
type ModuleSnapshot = {
|
2026-05-30 13:44:54 +09:00
|
|
|
restoreOriginalSpecifiers: boolean
|
|
|
|
|
restoreSpecifiers: Set<string>
|
|
|
|
|
restoreFactory: MockModuleFactory
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PersistentModuleSnapshot = {
|
|
|
|
|
ownerUrls: Set<string>
|
|
|
|
|
restoreSpecifiers: Set<string>
|
2026-04-10 15:52:59 +09:00
|
|
|
restoreFactory: MockModuleFactory
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ModuleMockLifecycleOptions = {
|
2026-05-30 13:44:54 +09:00
|
|
|
getCallerStack?: () => string
|
2026-04-10 15:52:59 +09:00
|
|
|
getCallerUrl?: () => string
|
2026-05-30 13:44:54 +09:00
|
|
|
trackOnlyDuringActiveTest?: boolean
|
2026-04-10 15:52:59 +09:00
|
|
|
resolveSpecifier?: (specifier: string, callerUrl: string) => string
|
|
|
|
|
loadOriginalModule?: (specifier: string, callerUrl: string) => ModuleLoadResult
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
let originalLoadNonce = 0
|
2026-04-10 15:52:59 +09:00
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function toError(error: unknown): Error {
|
|
|
|
|
return error instanceof Error ? error : new Error(String(error))
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 12:19:08 +09:00
|
|
|
function isModuleExports(moduleValue: unknown): moduleValue is Record<string, unknown> {
|
|
|
|
|
return moduleValue !== null && typeof moduleValue === "object"
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function isModuleNamespaceObject(moduleValue: Record<string, unknown>): boolean {
|
|
|
|
|
return Object.prototype.toString.call(moduleValue) === "[object Module]"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function shouldRestoreOriginalSpecifier(moduleValue: unknown): boolean {
|
|
|
|
|
return !isModuleExports(moduleValue) || !isModuleNamespaceObject(moduleValue)
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 12:19:08 +09:00
|
|
|
function createRestoreExports(moduleValue: unknown): Record<string, unknown> {
|
2026-04-10 15:52:59 +09:00
|
|
|
if (typeof moduleValue === "function") {
|
|
|
|
|
const functionExports = Object.assign({}, moduleValue)
|
|
|
|
|
return {
|
|
|
|
|
...functionExports,
|
|
|
|
|
default: moduleValue,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 12:19:08 +09:00
|
|
|
if (isModuleExports(moduleValue)) {
|
2026-05-30 13:44:54 +09:00
|
|
|
if (isModuleNamespaceObject(moduleValue)) {
|
|
|
|
|
return { ...moduleValue }
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 12:19:08 +09:00
|
|
|
return moduleValue
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { default: moduleValue }
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function resolveWithBun(specifier: string, callerUrl: string): string {
|
|
|
|
|
const callerDirectory = fileURLToPath(new URL(".", callerUrl))
|
|
|
|
|
return Bun.resolveSync(specifier, callerDirectory)
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function isSchemeSpecifier(specifier: string): boolean {
|
|
|
|
|
return /^[a-zA-Z][a-zA-Z\d+.-]*:/.test(specifier)
|
|
|
|
|
}
|
2026-04-10 15:52:59 +09:00
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function defaultResolveSpecifier(specifier: string, callerUrl: string): string {
|
|
|
|
|
try {
|
|
|
|
|
return resolveWithBun(specifier, callerUrl)
|
|
|
|
|
} catch {
|
|
|
|
|
return specifier
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function createOriginalLoadSpecifier(specifier: string, callerUrl: string): string {
|
2026-04-10 15:52:59 +09:00
|
|
|
try {
|
2026-05-30 13:44:54 +09:00
|
|
|
const resolved = resolveWithBun(specifier, callerUrl)
|
|
|
|
|
if (isSchemeSpecifier(resolved)) {
|
|
|
|
|
return specifier
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
originalLoadNonce += 1
|
|
|
|
|
return `${resolved}?omo_original=${originalLoadNonce}`
|
2026-04-10 15:52:59 +09:00
|
|
|
} catch {
|
|
|
|
|
return specifier
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function defaultLoadOriginalModule(specifier: string, callerUrl: string): ModuleLoadResult {
|
|
|
|
|
try {
|
|
|
|
|
const require = createRequire(callerUrl)
|
2026-05-30 13:44:54 +09:00
|
|
|
return { ok: true, value: require(createOriginalLoadSpecifier(specifier, callerUrl)) }
|
2026-04-10 15:52:59 +09:00
|
|
|
} catch (error) {
|
|
|
|
|
return { ok: false, error: toError(error) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function installModuleMockLifecycle(
|
|
|
|
|
mockApi: MockApi,
|
|
|
|
|
options: ModuleMockLifecycleOptions = {},
|
2026-05-30 13:44:54 +09:00
|
|
|
): {
|
|
|
|
|
beginTestMockTracking: () => void
|
|
|
|
|
endTestMockTracking: () => void
|
|
|
|
|
restoreModuleMocks: () => void
|
|
|
|
|
} {
|
2026-04-10 15:52:59 +09:00
|
|
|
const snapshots = new Map<string, ModuleSnapshot>()
|
2026-05-30 13:44:54 +09:00
|
|
|
const persistentSnapshots = new Map<string, PersistentModuleSnapshot>()
|
|
|
|
|
let lastRestoredSnapshots: ModuleSnapshot[] = []
|
|
|
|
|
let isActiveTest = !options.trackOnlyDuringActiveTest
|
2026-04-10 15:52:59 +09:00
|
|
|
const delegateModule = mockApi.module.bind(mockApi)
|
|
|
|
|
const delegateRestore = mockApi.restore.bind(mockApi)
|
2026-05-30 13:44:54 +09:00
|
|
|
const getCallerStack = options.getCallerStack ?? defaultGetCallerStack
|
2026-04-10 15:52:59 +09:00
|
|
|
const resolveSpecifier = options.resolveSpecifier ?? defaultResolveSpecifier
|
|
|
|
|
const loadOriginalModule = options.loadOriginalModule ?? defaultLoadOriginalModule
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function getCallerUrl(callerStack: string): string {
|
|
|
|
|
return options.getCallerUrl?.() ?? resolveCallerUrlFromStack(callerStack)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function restoreModuleMocksForRestoreCall(): void {
|
|
|
|
|
const snapshotsToRestore = snapshots.size > 0 ? Array.from(snapshots.values()) : lastRestoredSnapshots
|
|
|
|
|
|
|
|
|
|
for (const snapshot of snapshotsToRestore) {
|
|
|
|
|
for (const restoreSpecifier of snapshot.restoreSpecifiers) {
|
|
|
|
|
delegateModule(restoreSpecifier, snapshot.restoreFactory)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (snapshots.size > 0) {
|
|
|
|
|
lastRestoredSnapshots = snapshotsToRestore
|
|
|
|
|
snapshots.clear()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function restorePersistentModuleMocksForRestoreCall(): void {
|
|
|
|
|
for (const snapshot of persistentSnapshots.values()) {
|
|
|
|
|
for (const restoreSpecifier of snapshot.restoreSpecifiers) {
|
|
|
|
|
delegateModule(restoreSpecifier, snapshot.restoreFactory)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function clearPersistentModuleMocksForOwner(ownerUrl: string): void {
|
|
|
|
|
for (const [resolvedSpecifier, snapshot] of persistentSnapshots) {
|
|
|
|
|
snapshot.ownerUrls.delete(ownerUrl)
|
|
|
|
|
if (snapshot.ownerUrls.size === 0) {
|
|
|
|
|
persistentSnapshots.delete(resolvedSpecifier)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 15:52:59 +09:00
|
|
|
function restoreModuleMocks(): void {
|
2026-05-30 13:44:54 +09:00
|
|
|
if (snapshots.size === 0) {
|
|
|
|
|
return
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
restoreModuleMocksForRestoreCall()
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function beginTestMockTracking(): void {
|
|
|
|
|
isActiveTest = true
|
|
|
|
|
}
|
2026-04-10 15:52:59 +09:00
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
function endTestMockTracking(): void {
|
|
|
|
|
isActiveTest = !options.trackOnlyDuringActiveTest
|
|
|
|
|
}
|
2026-04-10 15:52:59 +09:00
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
mockApi.module = (specifier: string, factory: MockModuleFactory): unknown => {
|
|
|
|
|
lastRestoredSnapshots = []
|
|
|
|
|
const callerStack = getCallerStack()
|
|
|
|
|
const callerUrl = getCallerUrl(callerStack)
|
|
|
|
|
const isModuleEvaluation = isModuleEvaluationStack(callerStack)
|
|
|
|
|
|
|
|
|
|
if (isModuleEvaluation) {
|
|
|
|
|
const resolvedSpecifier = resolveSpecifier(specifier, callerUrl)
|
|
|
|
|
const existingSnapshot = persistentSnapshots.get(resolvedSpecifier)
|
|
|
|
|
if (existingSnapshot) {
|
|
|
|
|
existingSnapshot.ownerUrls.add(callerUrl)
|
|
|
|
|
existingSnapshot.restoreSpecifiers.add(specifier)
|
|
|
|
|
existingSnapshot.restoreSpecifiers.add(resolvedSpecifier)
|
|
|
|
|
} else {
|
|
|
|
|
persistentSnapshots.set(resolvedSpecifier, {
|
|
|
|
|
ownerUrls: new Set([callerUrl]),
|
|
|
|
|
restoreSpecifiers: new Set([specifier, resolvedSpecifier]),
|
|
|
|
|
restoreFactory: factory,
|
2026-04-10 15:52:59 +09:00
|
|
|
})
|
|
|
|
|
}
|
2026-05-30 13:44:54 +09:00
|
|
|
return delegateModule(specifier, factory)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isActiveTest) {
|
|
|
|
|
const resolvedSpecifier = resolveSpecifier(specifier, callerUrl)
|
|
|
|
|
const existingSnapshot = snapshots.get(resolvedSpecifier)
|
|
|
|
|
|
|
|
|
|
if (existingSnapshot) {
|
|
|
|
|
if (existingSnapshot.restoreOriginalSpecifiers) {
|
|
|
|
|
existingSnapshot.restoreSpecifiers.add(specifier)
|
|
|
|
|
}
|
|
|
|
|
existingSnapshot.restoreSpecifiers.add(resolvedSpecifier)
|
|
|
|
|
} else {
|
|
|
|
|
const originalModule = loadOriginalModule(specifier, callerUrl)
|
|
|
|
|
|
|
|
|
|
if (originalModule.ok) {
|
|
|
|
|
const restoreExports = createRestoreExports(originalModule.value)
|
|
|
|
|
const restoreOriginalSpecifiers = shouldRestoreOriginalSpecifier(originalModule.value)
|
|
|
|
|
snapshots.set(resolvedSpecifier, {
|
|
|
|
|
restoreOriginalSpecifiers,
|
|
|
|
|
restoreSpecifiers: new Set(
|
|
|
|
|
restoreOriginalSpecifiers ? [specifier, resolvedSpecifier] : [resolvedSpecifier],
|
|
|
|
|
),
|
|
|
|
|
restoreFactory: () => restoreExports,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return delegateModule(specifier, factory)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mockApi.restore = (): unknown => {
|
2026-05-30 13:44:54 +09:00
|
|
|
const callerStack = getCallerStack()
|
|
|
|
|
const callerUrl = getCallerUrl(callerStack)
|
2026-05-15 16:26:57 +09:00
|
|
|
const result = delegateRestore()
|
2026-05-30 13:44:54 +09:00
|
|
|
if (!isActiveTest) {
|
|
|
|
|
snapshots.clear()
|
|
|
|
|
lastRestoredSnapshots = []
|
|
|
|
|
clearPersistentModuleMocksForOwner(callerUrl)
|
|
|
|
|
restorePersistentModuleMocksForRestoreCall()
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
restoreModuleMocksForRestoreCall()
|
|
|
|
|
restorePersistentModuleMocksForRestoreCall()
|
2026-05-15 16:26:57 +09:00
|
|
|
return result
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|
|
|
|
|
|
2026-05-30 13:44:54 +09:00
|
|
|
return { beginTestMockTracking, endTestMockTracking, restoreModuleMocks }
|
2026-04-10 15:52:59 +09:00
|
|
|
}
|