bug: gamut_viewer.test.js fails with ReferenceError: window is not defined #212

Closed
opened 2026-09-06 12:35:22 +01:00 by gronod · 1 comment
Owner

Summary

Executing node src/js/gamut_viewer.test.js (documented in AGENTS.md under Frontend test suites) fails immediately with ReferenceError: window is not defined.

Details & Root Cause

  1. In src/js/gamut_viewer.js (lines 4–5):
    const { invoke } = window.__TAURI__.core;
    const { listen } = window.__TAURI__.event;
    
    Top-level code directly dereferences window.
  2. In src/js/gamut_viewer.test.js (line 5):
    import { parseGamutFile } from './gamut_viewer.js';
    
    Static ES import statements are hoisted and executed before any other statements. As a result, running the test in Node.js fails during module evaluation.
  3. Unlike src/js/profcheck.test.js and src/js/chartread.test.js, gamut_viewer.test.js lacks the globalThis.window polyfill and does not auto-run runAll() when executed directly from the Node.js CLI (process.argv[1]?.endsWith('gamut_viewer.test.js')).

Steps to Reproduce

Run the test command documented in AGENTS.md:

node src/js/gamut_viewer.test.js

Console output:

file:///Users/gordon/Projects/ICCery/src/js/gamut_viewer.js:4
const { invoke } = window.__TAURI__.core;
                   ^

ReferenceError: window is not defined
    at file:///Users/gordon/Projects/ICCery/src/js/gamut_viewer.js:4:20
    at ModuleJob.run (node:internal/modules/esm/module_job:343:25)

Proposed Remediation

  1. In src/js/gamut_viewer.test.js, add the Node environment window polyfill and dynamically import gamut_viewer.js (const { parseGamutFile } = await import('./gamut_viewer.js')), or safely guard typeof window !== 'undefined' in gamut_viewer.js.
  2. Add the Node CLI auto-run block at the end of src/js/gamut_viewer.test.js so node src/js/gamut_viewer.test.js executes and reports results (with non-zero exit code on failure) matching the other test suites.
### Summary Executing `node src/js/gamut_viewer.test.js` (documented in `AGENTS.md` under Frontend test suites) fails immediately with `ReferenceError: window is not defined`. ### Details & Root Cause 1. In `src/js/gamut_viewer.js` (lines 4–5): ```javascript const { invoke } = window.__TAURI__.core; const { listen } = window.__TAURI__.event; ``` Top-level code directly dereferences `window`. 2. In `src/js/gamut_viewer.test.js` (line 5): ```javascript import { parseGamutFile } from './gamut_viewer.js'; ``` Static ES import statements are hoisted and executed before any other statements. As a result, running the test in Node.js fails during module evaluation. 3. Unlike `src/js/profcheck.test.js` and `src/js/chartread.test.js`, `gamut_viewer.test.js` lacks the `globalThis.window` polyfill and does not auto-run `runAll()` when executed directly from the Node.js CLI (`process.argv[1]?.endsWith('gamut_viewer.test.js')`). ### Steps to Reproduce Run the test command documented in `AGENTS.md`: ```bash node src/js/gamut_viewer.test.js ``` Console output: ``` file:///Users/gordon/Projects/ICCery/src/js/gamut_viewer.js:4 const { invoke } = window.__TAURI__.core; ^ ReferenceError: window is not defined at file:///Users/gordon/Projects/ICCery/src/js/gamut_viewer.js:4:20 at ModuleJob.run (node:internal/modules/esm/module_job:343:25) ``` ### Proposed Remediation 1. In `src/js/gamut_viewer.test.js`, add the Node environment `window` polyfill and dynamically import `gamut_viewer.js` (`const { parseGamutFile } = await import('./gamut_viewer.js')`), or safely guard `typeof window !== 'undefined'` in `gamut_viewer.js`. 2. Add the Node CLI auto-run block at the end of `src/js/gamut_viewer.test.js` so `node src/js/gamut_viewer.test.js` executes and reports results (with non-zero exit code on failure) matching the other test suites.
gronod added the Kind/BugKind/Testing
Priority
High
2
labels 2026-09-06 12:35:22 +01:00
Author
Owner

Resolved in commit ec7eb2f on development.

  • Guarded window and window.__TAURI__ globals in src/js/gamut_viewer.js to allow headless evaluation without crashing on missing browser globals.
  • Added Node CLI polyfills for document, window, and WebGL/DOM stubs in src/js/gamut_viewer.test.js along with dynamic ESM import and test assertions runner.
  • All 8 unit tests in gamut_viewer.test.js pass cleanly under node src/js/gamut_viewer.test.js.
Resolved in commit `ec7eb2f` on `development`. - Guarded `window` and `window.__TAURI__` globals in `src/js/gamut_viewer.js` to allow headless evaluation without crashing on missing browser globals. - Added Node CLI polyfills for `document`, `window`, and WebGL/DOM stubs in `src/js/gamut_viewer.test.js` along with dynamic ESM import and test assertions runner. - All 8 unit tests in `gamut_viewer.test.js` pass cleanly under `node src/js/gamut_viewer.test.js`.
Sign in to join this conversation.