Wake-lock test: stop assigning to a global Node now owns

The first CI run found this, which is the point of having one. Node 21+ defines
globalThis.navigator itself as a getter with no setter, so `globalThis.navigator
= {...}` throws under ESM strict mode. This box runs Node 20, where the global
does not exist and the assignment is fine — the test has never failed locally
and never would.

Only the first test failed: its afterEach deletes the built-in accessor, so
every later test assigned cleanly against the hole it left. defineProperty works
on both runtimes.

Production images are node:22-alpine (web/Dockerfile, api/Dockerfile), so the
version CI runs is the right one and the test was what needed fixing.
This commit is contained in:
Duarte Santos committed 2026-07-31 19:21:03 +02:00
1 parent b45dbca2a6
commit 1574811af0
1 file changed
+10 -3
+10 -3
View File
@@ -5,6 +5,13 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
// otherwise the tests pass against a one-shot implementation that dies on the first tab switch.
let requests, released, live, listeners, reject
// Node 21+ defines globalThis.navigator itself, as a getter with no setter — a plain
// assignment throws under ESM's strict mode. defineProperty works on both that and the older
// runtimes where the global simply doesn't exist. Production images are node:22-alpine, so
// the tests have to run there too.
const setNavigator = value =>
Object.defineProperty(globalThis, 'navigator', { value, configurable: true, writable: true })
function fakeBrowser() {
requests = 0; released = 0; live = null; reject = false
listeners = new Set()
@@ -13,7 +20,7 @@ function fakeBrowser() {
addEventListener: (type, fn) => { if (type === 'visibilitychange') listeners.add(fn) },
removeEventListener: (type, fn) => { if (type === 'visibilitychange') listeners.delete(fn) },
}
globalThis.navigator = {
setNavigator({
wakeLock: {
request: async () => {
requests++
@@ -24,7 +31,7 @@ function fakeBrowser() {
return s
},
},
}
})
}
const fire = () => listeners.forEach(fn => fn())
const settle = () => new Promise(r => setTimeout(r, 0))
@@ -42,7 +49,7 @@ afterEach(() => { delete globalThis.document; delete globalThis.navigator })
describe('wakeLockSupported', () => {
it('is false when the browser has no wakeLock at all', () => {
globalThis.navigator = {}
setNavigator({})
expect(wakeLockSupported()).toBe(false)
})
it('is true when it does', () => {