// Resolve o alias "@/" e imports sem extensão do projeto, para que os testes // importem módulos reais (inclusive Route Handlers do Next.js) com o type // stripping nativo do Node. Requer Node >= 22.15 (module.registerHooks). import { existsSync, statSync } from "node:fs"; import { registerHooks } from "node:module"; import { fileURLToPath, pathToFileURL } from "node:url"; const ROOT = new URL("../../", import.meta.url); const SUFFIXES = ["", ".ts", ".tsx", ".js", "/index.ts", "/index.js"]; function findFile(base) { for (const suffix of SUFFIXES) { const candidate = base + suffix; if (existsSync(candidate) && statSync(candidate).isFile()) return candidate; } return null; } registerHooks({ resolve(specifier, context, nextResolve) { if (specifier.startsWith("@/")) { const found = findFile(fileURLToPath(new URL(specifier.slice(2), ROOT))); if (found) return nextResolve(pathToFileURL(found).href, context); } try { return nextResolve(specifier, context); } catch (error) { const code = error?.code; if (code !== "ERR_MODULE_NOT_FOUND" && code !== "ERR_UNSUPPORTED_DIR_IMPORT") throw error; if (specifier.startsWith(".") && context.parentURL) { const found = findFile(fileURLToPath(new URL(specifier, context.parentURL))); if (found) return nextResolve(pathToFileURL(found).href, context); } // Subcaminho de pacote CJS sem exports, como "next/server". if (!specifier.startsWith(".") && !specifier.startsWith("node:")) { return nextResolve(`${specifier}.js`, context); } throw error; } }, });