git diff origin/develop -- utilities/rest.test.jsThe 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 ''.
createQueryParams() (lines 8–39)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() (lines 41–70)Three test cases:
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 handleHTTPErrors() function (rest.js:32-38) has no unit test here — it's exercised indirectly through the authentication.test.js tests via vi.fn() mocking. A direct unit test for handleHTTPErrors would be a good addition.
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. This is 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'→''.But fixing a real bug. Here's why.