From c4c0ec37a71115678faee1e23b4c2088b073d42b Mon Sep 17 00:00:00 2001 From: thePR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> Date: Sat, 11 Oct 2025 21:03:35 -0400 Subject: [PATCH] chore: polyfill localStorage for node tests --- package.json | 1 + scripts/run-unit-tests.mjs | 3 ++- tests/test-helpers/setup-localstorage.mjs | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 51782d83..af876504 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "bitvid", "private": true, + "type": "module", "version": "1.0.0", "scripts": { "build:css": "npx tailwindcss -c tailwind.config.cjs -i css/tailwind.source.css -o css/tailwind.generated.css --minify", diff --git a/scripts/run-unit-tests.mjs b/scripts/run-unit-tests.mjs index ff860c0e..f5ac109d 100644 --- a/scripts/run-unit-tests.mjs +++ b/scripts/run-unit-tests.mjs @@ -6,6 +6,7 @@ import path from "node:path"; const rootDir = path.dirname(fileURLToPath(new URL("../package.json", import.meta.url))); const testsDir = path.join(rootDir, "tests"); const testFiles = []; +const setupImport = new URL("../tests/test-helpers/setup-localstorage.mjs", import.meta.url); async function collectTests(dir) { const entries = await readdir(dir, { withFileTypes: true }); @@ -49,7 +50,7 @@ for (const testFile of testFiles) { console.log(`\n→ Running ${relativePath}`); await new Promise((resolve, reject) => { - const child = spawn(process.execPath, [testFile], { + const child = spawn(process.execPath, ["--import", setupImport.href, testFile], { stdio: "inherit", }); diff --git a/tests/test-helpers/setup-localstorage.mjs b/tests/test-helpers/setup-localstorage.mjs index c990b187..ff110ed8 100644 --- a/tests/test-helpers/setup-localstorage.mjs +++ b/tests/test-helpers/setup-localstorage.mjs @@ -1,3 +1,7 @@ +if (typeof globalThis.window === "undefined") { + globalThis.window = {}; +} + if (typeof globalThis.localStorage === "undefined") { const store = new Map(); globalThis.localStorage = { @@ -15,3 +19,7 @@ if (typeof globalThis.localStorage === "undefined") { }, }; } + +if (typeof globalThis.window.localStorage === "undefined") { + globalThis.window.localStorage = globalThis.localStorage; +}