test: the theme drives the colour scheme, and mockModule takes the module

Both of Greptile's findings on #591 were real, and the first was a plain miss:
mockModule shipped with no caller, so its documented usage had never once been
executed and could not have worked.  It called jest.requireActual on a path
passed in by the spec, which resolves relative to test-utils.js rather than to
the caller -- for any spec in a subdirectory the documented
'../hooks/customHooks' pointed outside src altogether.  It now takes the module
the spec has already resolved, and throws on a string rather than quietly
resolving to the wrong one, which was the original failure.

It also has a caller now.  FileBrowser.test.js listed six hooks in a hand-written
factory and returned only those, so every other export of customHooks was
undefined for anything it rendered -- the exact landmine the helper exists to
remove.

The second: `inverted` drove Mantine's colour scheme on its own, so asking for
night or amber through the new theme option produced a tree that reported a dark
theme while Mantine rendered its light one.  Production cannot reach that
combination, and a harness that invents states the app never has is worse than
one that is merely incomplete -- what a test proves about such a tree is true of
nothing.  The scheme is now derived from the theme name via isDarkTheme, and
data-theme is stamped on <html>, without which a component test cannot exercise
any token table or the night-mode treatment at all.  isDark remains overridable
so a spec can still make the two disagree deliberately.

test-utils.test.js covers the harness itself, which had none.  Eight of its
twelve assertions were red against the code above.

986 jest, 46 e2e, clean cache-busted build, tsc clean.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-07-30 16:39:30 -06:00
parent 17d658fc0b
commit 40a3886eaf
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
3 changed files with 156 additions and 21 deletions

View file

@ -32,6 +32,7 @@ import {
ThemeContext,
} from './contexts/contexts';
import {cssVariablesResolver, mantineTheme} from './themes/mantine';
import {darkTheme, isDarkTheme, lightTheme} from './themes/names';
import {
domainFixture,
domainsFixture,
@ -127,12 +128,28 @@ export function renderWithProviders(
// this by hand.
if (route) window.history.pushState({}, '', route);
/*
* The theme decides the colour scheme, rather than the two being set independently.
*
* `inverted` used to drive Mantine's scheme on its own, so asking for night or amber
* through the theme option produced a tree that reported a dark theme while Mantine
* rendered its light one -- a combination production cannot reach. A harness inventing
* states the app never has is worse than one that is merely incomplete: what a test proves
* about that tree is true of nothing.
*
* `isDark` follows from the theme name, but only as a default: a spec deliberately checking
* what a component does when the two disagree can still say so.
*/
const requestedTheme = theme.theme ?? themeContext.theme ?? (inverted ? darkTheme : lightTheme);
const themeValue = themeContextFixture({
theme: inverted ? 'dark' : 'light',
isDark: inverted,
theme: requestedTheme,
isDark: isDarkTheme(requestedTheme),
...themeContext,
...theme,
});
// Every token table and the whole night-mode treatment key on this attribute, so a
// component test that leaves it unset cannot exercise any of them.
document.documentElement.dataset.theme = requestedTheme;
const statusValue = statusContextFixture(status);
const settingsValue = settingsContextFixture(settings);
const queryValue = queryContextFixture(query);
@ -170,7 +187,7 @@ export function renderWithProviders(
<MantineProvider
theme={mantineTheme}
cssVariablesResolver={cssVariablesResolver}
forceColorScheme={inverted ? 'dark' : 'light'}
forceColorScheme={isDarkTheme(requestedTheme) ? 'dark' : 'light'}
>
{withProviders}
</MantineProvider>
@ -192,20 +209,33 @@ export function renderWithProviders(
/**
* Replace named exports of a module, keeping every other export intact.
*
* Use inside a `jest.mock` factory:
* Takes the module, which the spec resolves itself, not a path to it:
*
* jest.mock('../hooks/customHooks', () =>
* require('../test-utils').mockModule('../hooks/customHooks', {
* useDomains: () => mockUseDomains(),
* }));
* require('../test-utils').mockModule(
* jest.requireActual('../hooks/customHooks'),
* {useDomains: () => mockUseDomains()},
* ));
*
* The point is the spread. A factory written by hand returns only the keys it lists, so
* every other export of that module becomes undefined -- one spec mocks six hooks out of
* customHooks and silently blanks the rest, and the next hook a component under it starts
* calling fails as "not a function" a long way from the cause.
* The point is the spread. A factory written by hand returns only the keys it lists, so every
* other export of that module becomes undefined -- one spec mocks six hooks out of customHooks
* and blanks the rest, and the next hook a component under it starts calling fails as "not a
* function" a long way from the cause.
*
* It first took a path and called `jest.requireActual` itself, which cannot work: the
* specifier would resolve relative to THIS file rather than to the spec, so the documented
* `'../hooks/customHooks'` pointed outside src altogether for any spec in a subdirectory. The
* helper had no caller, so nothing ever ran it. Throwing on a string is deliberate -- silently
* resolving to the wrong module is the failure that was there to begin with.
*/
export function mockModule(modulePath, overrides) {
return {...jest.requireActual(modulePath), ...overrides};
export function mockModule(actualModule, overrides) {
if (typeof actualModule === 'string') {
throw new Error(
'mockModule takes the module, not a path: pass jest.requireActual(path) so the '
+ 'specifier resolves from your spec rather than from test-utils.js',
);
}
return {...actualModule, ...overrides};
}
/*