// Module syntax detection for ambiguous files: a .js or .ts whose nearest package.json // has no "type". The goldens are what Node v24.16 printed for the same guests under // tests/fixtures/guests/detect, kept in tests/goldens/detect/ by `node scripts/golden.mjs`. import { test, expect } from "@playwright/test"; import { goldenSuite, runGuest } from "./guest"; goldenSuite(["detect/"]); async function guestFiles(page: import("@playwright/test").Page, names: string[]): Promise> { const files: Record = {}; for (const name of names) { const res = await page.request.get(`/tests/fixtures/guests/${name}`); expect(res.ok()).toBe(true); files[`/app/${name}`] = await res.text(); } return files; } test("a file with no package.json above it is detected without a warning", async ({ page }) => { // Node v24.16, run in a directory with no package.json in any ancestor: ESM, silent. const out = await runGuest(page, { "/nopkg/main.js": "export {};\nconsole.log(import.meta.url);\n" }, "/nopkg/main.js"); expect(out).toEqual({ code: 0, signal: null, stdout: "file:///nopkg/main.js\n", stderr: "" }); }); test("an explicit commonjs type turns detection off", async ({ page }) => { // Node: SyntaxError at link time, "Named export 'greet' not found … is a CommonJS // module" (its wording; the engine's own link error names the same export). const files = await guestFiles(page, ["detect-cjs/import.mjs", "detect-cjs/package.json", "detect-cjs/greet.js"]); const out = await runGuest(page, files, "/app/detect-cjs/import.mjs"); expect(out.code).toBe(1); expect(out.stderr).toContain("SyntaxError"); expect(out.stderr).toContain("'greet'"); expect(out.stderr).not.toContain("MODULE_TYPELESS_PACKAGE_JSON"); });