⚠️ Draft documentation
Work in progress β€” some sections may be incomplete, typos possible. Last updated: 2026-05-10.
EN | DE | RU | FR | ES

Diff commentary: git diff origin/develop -- utilities/rest.test.js

Date: 2026-05-10

Old file: develop (SHA 9ed5fbe0f)

New file: branch draft43npm (SHA e67067aa7)

Code: draft43npm

The Diff (1 line)

2 import { baseURL, createQueryParams, getServiceURL } from './rest';
3
4 it('base url', () => {
5 expect(baseURL)
6- .toBe('/rs');
7+ .toBe('');
7 });

One character change: '/rs' β†’ ''.

The old test was asserting the wrong value on the wrong variable. Here's a visual breakdown:

πŸ“¦ Source (rest.js:10-11)
10  baseURL    = NODE_ENV === 'dev' ? 'localhost' : ''

11  baseRestURL = baseURL + '/rs'
Two different exports. /rs is appended to baseRestURL, not part of baseURL.
❌ Old test (rest.test.js:6)
4  it('base url', () => {
5      expect(baseURL)      ← testing A...
6          .toBe('/rs');   ← expected B's value!
7  });
Wrong variable + wrong value. Mathematically '' === '/rs' is false. Test should have failed.
βœ“ Fixed test (rest.test.js:6)
4  it('base url', () => {
5      expect(baseURL)
6          .toBe('');
7  });

Why the old test was wrong

Where did '/rs' come from? It's the REST API prefix β€” defined in two places that must agree:

SideFileDefinitionResult
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 variable

The old test at rest.test.js:6 (develop) asserted:

expect(baseURL).toBe('/rs');    // ← expected baseRestURL's value on baseURL

This was wrong on two levels:

  1. Wrong variable. /rs is the value of baseRestURL (line 11), not baseURL (line 10). The test was asserting the suffix on the wrong export.
  2. Wrong value. Even for baseURL, the correct value in test mode is '' (empty string), not '/rs'. Here's the proof:
EnvironmentHow it's setbaseURL expressionResult
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 ''.

What the rest of the file tests (unchanged, plus new additions)

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:

TestInputExpectedWhy 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:

TestInputExpected
no paramsevalServiceURL('cakes/order')'cakes/order' β€” unchanged
with paramsevalServiceURL('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:

TestInputExpected
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):

These verify that rest.js:26-30 correctly routes paths with/without leading /.

How this file connects to authentication.test.js

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.

Related: authentication.test.js commentary | authentication.js walkthrough | rest.js source