fix(testing): preserve mock restore export identity

This commit is contained in:
YeonGyu-Kim
2026-05-30 12:19:08 +09:00
parent d32caf3d87
commit 68f02e61ef
2 changed files with 19 additions and 14 deletions
+9 -5
View File
@@ -31,7 +31,11 @@ function toError(error: unknown): Error {
return new Error(String(error))
}
function cloneModuleExports(moduleValue: unknown): Record<string, unknown> {
function isModuleExports(moduleValue: unknown): moduleValue is Record<string, unknown> {
return moduleValue !== null && typeof moduleValue === "object"
}
function createRestoreExports(moduleValue: unknown): Record<string, unknown> {
if (typeof moduleValue === "function") {
const functionExports = Object.assign({}, moduleValue)
return {
@@ -40,8 +44,8 @@ function cloneModuleExports(moduleValue: unknown): Record<string, unknown> {
}
}
if (moduleValue && typeof moduleValue === "object") {
return { ...(moduleValue as Record<string, unknown>) }
if (isModuleExports(moduleValue)) {
return moduleValue
}
return { default: moduleValue }
@@ -123,10 +127,10 @@ export function installModuleMockLifecycle(
const originalModule = loadOriginalModule(specifier, callerUrl)
if (originalModule.ok) {
const clonedExports = cloneModuleExports(originalModule.value)
const restoreExports = createRestoreExports(originalModule.value)
snapshots.set(restoreSpecifier, {
restoreSpecifier,
restoreFactory: () => ({ ...clonedExports }),
restoreFactory: () => restoreExports,
})
}
}