refactor(testing): extract mock restore exports

This commit is contained in:
YeonGyu-Kim
2026-05-30 16:04:45 +09:00
parent 51fd474274
commit 7e1cfbfdc2
2 changed files with 28 additions and 28 deletions
+1 -28
View File
@@ -1,6 +1,7 @@
import { createRequire } from "node:module"
import { fileURLToPath } from "node:url"
import { defaultGetCallerStack, resolveCallerUrlFromStack } from "./module-mock-stack"
import { createRestoreExports } from "./module-mock-restore-exports"
type MockModuleFactory = () => Record<string, unknown>
@@ -40,34 +41,6 @@ function toError(error: unknown): Error {
return error instanceof Error ? error : new Error(String(error))
}
function isModuleExports(moduleValue: unknown): moduleValue is Record<string, unknown> {
return moduleValue !== null && typeof moduleValue === "object"
}
function isModuleNamespaceObject(moduleValue: Record<string, unknown>): boolean {
return Object.prototype.toString.call(moduleValue) === "[object Module]"
}
function createRestoreExports(moduleValue: unknown): Record<string, unknown> {
if (typeof moduleValue === "function") {
const functionExports = Object.assign({}, moduleValue)
return {
...functionExports,
default: moduleValue,
}
}
if (isModuleExports(moduleValue)) {
if (isModuleNamespaceObject(moduleValue)) {
return { ...moduleValue }
}
return moduleValue
}
return { default: moduleValue }
}
function resolveWithBun(specifier: string, callerUrl: string): string {
const callerDirectory = fileURLToPath(new URL(".", callerUrl))
return Bun.resolveSync(specifier, callerDirectory)
@@ -0,0 +1,27 @@
function isModuleExports(moduleValue: unknown): moduleValue is Record<string, unknown> {
return moduleValue !== null && typeof moduleValue === "object"
}
function isModuleNamespaceObject(moduleValue: Record<string, unknown>): boolean {
return Object.prototype.toString.call(moduleValue) === "[object Module]"
}
export function createRestoreExports(moduleValue: unknown): Record<string, unknown> {
if (typeof moduleValue === "function") {
const functionExports = Object.assign({}, moduleValue)
return {
...functionExports,
default: moduleValue,
}
}
if (isModuleExports(moduleValue)) {
if (isModuleNamespaceObject(moduleValue)) {
return { ...moduleValue }
}
return moduleValue
}
return { default: moduleValue }
}