Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
tests: happy-dom tests get a working localStorage on Node 24+ (#733)
Duarte Santos21145aa103a0
diff --git a/frontend/src/test-setup.js b/frontend/src/test-setup.jsnew file mode 100644index 000000000..c83ae00c2--- /dev/null+++ b/frontend/src/test-setup.js@@ -0,0 +1,31 @@+// Vitest setup, run before every test file (vite.config.js test.setupFiles).+//+// Node 24 and later ship a Web Storage of their own: `localStorage` sits on the global and is+// unusable unless node was started with --localstorage-file (undefined, or a getter that throws).+// Vitest's happy-dom environment does not overwrite a global that already exists, so a happy-dom+// test got Node's instead of happy-dom's and the first getItem threw: 565 failures on Node 26+// (#733). Node 22, which CI uses, has none, and nothing here changes anything there.+//+// The stand-in is happy-dom's own Storage, and the `Storage` global goes with it: the multi-tab+// and quota tests patch Storage.prototype.setItem, and Node's Storage class would leave that+// patch on a prototype nobody uses.+import { Storage } from 'happy-dom'++// `in`, not reading s.getItem: happy-dom binds a method onto the instance the first time it is+// read, and then a test that patches Storage.prototype.setItem (the multi-tab and quota tests)+// never sees the calls.+const usable = s => s != null && typeof s === 'object' && 'getItem' in s && 'setItem' in s++export function ensureWebStorage(g = globalThis) {+ // Only a DOM environment; a node-environment test that wants storage brings its own stub.+ if (typeof g.document === 'undefined') return+ for (const key of ['localStorage', 'sessionStorage']) {+ let current+ try { current = g[key] } catch { current = null }+ if (usable(current)) continue+ Object.defineProperty(g, key, { value: new Storage(), configurable: true, writable: true })+ if (g.Storage !== Storage) Object.defineProperty(g, 'Storage', { value: Storage, configurable: true, writable: true })+ }+}++ensureWebStorage()diff --git a/frontend/src/test-setup.test.js b/frontend/src/test-setup.test.jsnew file mode 100644index 000000000..ddba530e9--- /dev/null+++ b/frontend/src/test-setup.test.js@@ -0,0 +1,33 @@+// @vitest-environment happy-dom+import { describe, expect, it } from 'vitest'+import { ensureWebStorage } from './test-setup.js'++// What Node 24+ leaves on the global without --localstorage-file (#733): a localStorage that is+// undefined, or one whose getter throws.+describe('ensureWebStorage', () => {+ it('replaces a localStorage that is undefined or throws, and leaves a working one alone', () => {+ const g = { document: {} }+ Object.defineProperty(g, 'localStorage', { get() { return undefined }, configurable: true })+ Object.defineProperty(g, 'sessionStorage', { get() { throw new TypeError('invalid localStorage location') }, configurable: true })+ ensureWebStorage(g)+ g.localStorage.setItem('k', 'v')+ expect(g.localStorage.getItem('k')).toBe('v')+ g.sessionStorage.setItem('s', '1')+ expect(g.sessionStorage.getItem('s')).toBe('1')+ // Storage.prototype patches (the multi-tab tests) reach the stand-in.+ expect(g.localStorage).toBeInstanceOf(g.Storage)+ const kept = g.localStorage+ ensureWebStorage(g)+ expect(g.localStorage).toBe(kept)+ })+ it('does nothing outside a DOM environment', () => {+ const g = {}+ ensureWebStorage(g)+ expect('localStorage' in g).toBe(false)+ })+ it('the test environment itself has a working localStorage', () => {+ localStorage.setItem('x', '1')+ expect(localStorage.getItem('x')).toBe('1')+ localStorage.removeItem('x')+ })+})diff --git a/frontend/vite.config.js b/frontend/vite.config.jsindex dead38b12..77a53bbf4 100644--- a/frontend/vite.config.js+++ b/frontend/vite.config.js@@ -116,5 +116,7 @@ export default defineConfig({} : {})}},- build: { chunkSizeWarningLimit: 1500 }+ build: { chunkSizeWarningLimit: 1500 },+ // Gives happy-dom tests a working localStorage on Node 24+ (#733).+ test: { setupFiles: ['./src/test-setup.js'] }})