git diff origin/develop -- utilities/rest.test.jsWhere did '/rs' come from? It's the REST API prefix β defined in two places that must agree:
| Side | File | Definition | Result |
|---|---|---|---|
| Backend (Java) | RestPaths.java:31 |
public static final String REST = "rs"; |
β Rest.URL = "/rs" |
| Frontend (JS) | rest.js:11 |
export const baseRestURL = \`${baseURL}/rs\`; |
β "/rs" (when baseURL = "") |
The suffix "/rs" lives on baseRestURL β it's never part of baseURL. The old test was checking $BASE_REST_URL's value on $BASE_URL's name β like testing window.location.pathname and expecting window.location.host's value.
The old source code at rest.js:10-11 (develop) defines two different exports:
export const baseURL = process.env.NODE_ENV === 'development' ? testServer : ''; // β this file
export const baseRestURL = `${baseURL}/rs`; // β NOT the same variableThe old test at rest.test.js:6 (develop) asserted:
expect(baseURL).toBe('/rs'); // β expected baseRestURL's value on baseURLThis was wrong on two levels:
/rs is the value of baseRestURL (line 11), not baseURL (line 10). The test was asserting the suffix on the wrong export.baseURL, the correct value in test mode is '' (empty string), not '/rs'. Here's the proof:| Environment | How it's set | baseURL expression | Result |
|---|---|---|---|
Dev (npm start) |
CRA sets NODE_ENV='development' |
'development' === 'development' β true |
'http://localhost:8080' |
Test (npm test) |
CRA sets NODE_ENV='test' β injected by react-scripts test via Jest's testEnvironment |
'test' === 'development' β false |
'' (empty) |
Production (npm run build) |
CRA sets NODE_ENV='production' |
'production' === 'development' β false |
'' (empty) |
So in test mode, the expression always evaluated to ''. The old test expect(baseURL).toBe('/rs') was asserting '' === '/rs' β which is mathematically false. Yet the test passed for years.
Why did it pass? The test was running in CRA's Jest environment. CRA's react-scripts test wraps Jest and sets various global variables. It's possible that in some CRA versions, NODE_ENV was not consistently set to 'test' before the test file was evaluated β or the test was cached and never re-run after the source change. Regardless, the assertion was logically incorrect: it tested baseRestURL's value on baseURL's name.
The fix: With Vite, import.meta.env.MODE is always 'test' during vitest run. The source at rest.js:10 (new) reads:
export const baseURL = (import.meta.env.DEV && import.meta.env.MODE !== 'test') ? testServer : '';
In test mode: import.meta.env.DEV = false, MODE = 'test'. Expression: false && ('test' !== 'test') β false && false β false. baseURL = ''. The new test at rest.test.js:6 correctly expects ''.
baseRestURL (line 10)New direct test: expect(baseRestURL).toBe('/rs'). This is the expression \`${baseURL}/rs\` β the suffix that the old baseURL test accidentally checked. Now tested explicitly.
handleHTTPErrors() (lines 14β26)New describe block with three test cases for rest.js:32-38:
| Test | Input | Expected | Why it matters |
|---|---|---|---|
| non-OK response (401) | { ok: false, status: 401 } |
throws Error('Fetch failed: Error 401') |
This is the exact error that authentication.test.js:88 expects for wrong passwords |
| non-OK response (500) | { ok: false, status: 500 } |
throws Error('Fetch failed: Error 500') |
Verifies the error message uses the actual status code, not a hardcoded value |
| OK response | { ok: true, status: 200 } |
returns the same object unchanged | Happy path β function is a pass-through for successful responses |
Uses expect(() => fn()).toThrow() β a Vitest pattern that wraps the call to catch the thrown error.
evalServiceURL() (lines 28β39)New describe block testing the URL assembly helper rest.js:18-24. Three cases:
| Test | Input | Expected |
|---|---|---|
| no params | evalServiceURL('cakes/order') | 'cakes/order' β unchanged |
| with params | evalServiceURL('cakes/order', { id: 1, amount: 123 }) | 'cakes/order?id=1&amount=123' |
URL already has ? | evalServiceURL('cakes/order?page=1', { amount: 10 }) | 'cakes/order?page=1&amount=10' β uses & separator |
This function is called internally by getServiceURL() at rest.js:29. The key test is the third one β verifying that when a URL already has query parameters, new ones are appended with & (rest.js:20) instead of the second ?.
createQueryParams() (lines 42β55)Four test cases for building URL query strings from objects:
| Test | Input | Expected |
|---|---|---|
| empty params | {} | '' |
| one param | { amount: 10 } | 'amount=10' |
| several params | { amount: 10, flavor: 'sweet' } | 'amount=10&flavor=sweet' |
| URI encoding | { name: 'SchwarzwΓ€lder Kirschtorte' } | 'name=Schwarzw%C3%A4lder...' (encoded) |
These verify that rest.js:13-16 correctly filters undefined values and encodes URI components via encodeURIComponent().
getServiceURL()Three test cases (unchanged):
getServiceURL('cakes/order') β '/rs/cakes/order' β path without leading / gets prefixed with baseRestURL/getServiceURL('cakes/order', {}) β '/rs/cakes/order' β empty object produces no query stringgetServiceURL('cakes/order', { id: 1, amount: 123 }) β '/rs/cakes/order?id=1&amount=123' β params appendedThese verify that rest.js:26-30 correctly routes paths with/without leading /.
The functions tested here are the only two utilities imported by authentication.js:1:
import { getServiceURL, handleHTTPErrors } from '../utilities/rest';getServiceURL β converts '/rsPublic/login' to either 'http://localhost:8080/rsPublic/login' (dev) or '/rsPublic/login' (prod). This test verifies the devβprod URL logic.
handleHTTPErrors β throws Error('Fetch failed: Error 401') on non-OK responses. Now has its own direct unit test in this file (lines 18β23). It's also the exact error string that authentication.test.js expects at line 88.
The baseURL change (process.env.NODE_ENV β import.meta.env) is what made the whole Vite migration work β without it, every fetch() in the app would fail in dev mode.
One character change:
'/rs'β''.The old test was asserting the wrong value on the wrong variable. Here's a visual breakdown:
/rsis appended tobaseRestURL, not part ofbaseURL.'' === '/rs'is false. Test should have failed.