refactor(testing): extract mock restore exports
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { createRequire } from "node:module"
|
import { createRequire } from "node:module"
|
||||||
import { fileURLToPath } from "node:url"
|
import { fileURLToPath } from "node:url"
|
||||||
import { defaultGetCallerStack, resolveCallerUrlFromStack } from "./module-mock-stack"
|
import { defaultGetCallerStack, resolveCallerUrlFromStack } from "./module-mock-stack"
|
||||||
|
import { createRestoreExports } from "./module-mock-restore-exports"
|
||||||
|
|
||||||
type MockModuleFactory = () => Record<string, unknown>
|
type MockModuleFactory = () => Record<string, unknown>
|
||||||
|
|
||||||
@@ -40,34 +41,6 @@ function toError(error: unknown): Error {
|
|||||||
return error instanceof Error ? error : new Error(String(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 {
|
function resolveWithBun(specifier: string, callerUrl: string): string {
|
||||||
const callerDirectory = fileURLToPath(new URL(".", callerUrl))
|
const callerDirectory = fileURLToPath(new URL(".", callerUrl))
|
||||||
return Bun.resolveSync(specifier, callerDirectory)
|
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 }
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user